From 2f3404195cf188dd1bf15c65a2e6e2af77b45b3b Mon Sep 17 00:00:00 2001 From: Kingsmedia Date: Sat, 28 May 2022 13:51:52 +0200 Subject: [PATCH] 203 - Cheating and Validation --- Source/Blaster/Components/CombatComponent.cpp | 24 +++++++++++++++---- Source/Blaster/Components/CombatComponent.h | 8 +++---- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/Source/Blaster/Components/CombatComponent.cpp b/Source/Blaster/Components/CombatComponent.cpp index 8637b96..5d0c0f5 100644 --- a/Source/Blaster/Components/CombatComponent.cpp +++ b/Source/Blaster/Components/CombatComponent.cpp @@ -129,7 +129,7 @@ void UCombatComponent::FireProjectileWeapon() { HitTarget = PrimaryWeapon->bUseScatter ? PrimaryWeapon->TraceEndWithScatter(HitTarget) : HitTarget; if (!Character->HasAuthority()) LocalFire(HitTarget); - ServerFire(HitTarget); + ServerFire(HitTarget, PrimaryWeapon->FireDelay); } } @@ -139,7 +139,7 @@ void UCombatComponent::FireHitScanWeapon() { HitTarget = PrimaryWeapon->bUseScatter ? PrimaryWeapon->TraceEndWithScatter(HitTarget) : HitTarget; if (!Character->HasAuthority()) LocalFire(HitTarget); - ServerFire(HitTarget); + ServerFire(HitTarget, PrimaryWeapon->FireDelay); } } @@ -151,7 +151,7 @@ void UCombatComponent::FireShotgun() TArray HitTargets; Shotgun->ShotgunTraceEndWithScatter(HitTarget, HitTargets); if (!Character->HasAuthority()) ShotgunLocalFire(HitTargets); - ServerShotgunFire(HitTargets); + ServerShotgunFire(HitTargets, PrimaryWeapon->FireDelay); } } @@ -177,22 +177,34 @@ void UCombatComponent::FireTimerFinished() ReloadEmptyWeapon(); } -void UCombatComponent::ServerFire_Implementation(const FVector_NetQuantize& TraceHitTarget) +void UCombatComponent::ServerFire_Implementation(const FVector_NetQuantize& TraceHitTarget, float FireDelay) { MulticastFire(TraceHitTarget); } +bool UCombatComponent::ServerFire_Validate(const FVector_NetQuantize& TraceHitTarget, float FireDelay) +{ + if (PrimaryWeapon == nullptr) return true; + return FMath::IsNearlyEqual(PrimaryWeapon->FireDelay, FireDelay, 0.001f); +} + void UCombatComponent::MulticastFire_Implementation(const FVector_NetQuantize& TraceHitTarget) { if (Character && Character->IsLocallyControlled() && !Character->HasAuthority()) return; LocalFire(TraceHitTarget); } -void UCombatComponent::ServerShotgunFire_Implementation(const TArray& TraceHitTargets) +void UCombatComponent::ServerShotgunFire_Implementation(const TArray& TraceHitTargets, float FireDelay) { MulticastShotgunFire(TraceHitTargets); } +bool UCombatComponent::ServerShotgunFire_Validate(const TArray& TraceHitTargets, float FireDelay) +{ + if (PrimaryWeapon == nullptr) return true; + return FMath::IsNearlyEqual(PrimaryWeapon->FireDelay, FireDelay, 0.001f); +} + void UCombatComponent::MulticastShotgunFire_Implementation(const TArray& TraceHitTargets) { if (Character && Character->IsLocallyControlled() && !Character->HasAuthority()) return; @@ -823,6 +835,8 @@ bool UCombatComponent::CanFire() void UCombatComponent::OnRep_CarriedAmmo() { + if (Character == nullptr) return; + Controller = Controller == nullptr ? Cast(Character->Controller) : Controller; if (Controller) { diff --git a/Source/Blaster/Components/CombatComponent.h b/Source/Blaster/Components/CombatComponent.h index 8073d13..cc89f42 100644 --- a/Source/Blaster/Components/CombatComponent.h +++ b/Source/Blaster/Components/CombatComponent.h @@ -71,14 +71,14 @@ protected: void FireHitScanWeapon(); void FireShotgun(); - UFUNCTION(Server, Reliable) - void ServerFire(const FVector_NetQuantize& TraceHitTarget); + UFUNCTION(Server, Reliable, WithValidation) + void ServerFire(const FVector_NetQuantize& TraceHitTarget, float FireDelay); UFUNCTION(NetMulticast, Reliable) void MulticastFire(const FVector_NetQuantize& TraceHitTarget); - UFUNCTION(Server, Reliable) - void ServerShotgunFire(const TArray& TraceHitTargets); + UFUNCTION(Server, Reliable, WithValidation) + void ServerShotgunFire(const TArray& TraceHitTargets, float FireDelay); UFUNCTION(NetMulticast, Reliable) void MulticastShotgunFire(const TArray& TraceHitTargets);