Skip to content

Commit

Permalink
#255 Added GetInaccuracy method
Browse files Browse the repository at this point in the history
  • Loading branch information
xthebat committed Apr 5, 2024
1 parent 4339774 commit 50015c3
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Source/Cloud9/Character/Effects/Cloud9CharacterEffect.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Copyright (c) 2024 Alexei Gladkikh


#include "Cloud9CharacterEffect.h"
25 changes: 25 additions & 0 deletions Source/Cloud9/Character/Effects/Cloud9CharacterEffect.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2024 Alexei Gladkikh

#pragma once

#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "Cloud9CharacterEffect.generated.h"

class ACloud9Character;

UINTERFACE()
class UCloud9CharacterEffect : public UInterface
{
GENERATED_BODY()
};


class CLOUD9_API ICloud9CharacterEffect
{
GENERATED_BODY()

public:
UFUNCTION(BlueprintCallable, BlueprintNativeEvent)
bool Apply(ACloud9Character* Character);
};
24 changes: 24 additions & 0 deletions Source/Cloud9/Cloud9Consts.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) 2024 Alexei Gladkikh

#pragma once

/**
* See cs_shareddefs.cpp in cstrike15_src
*/
namespace Cloud9Player
{
constexpr float SpeedRun = 260.0f;
constexpr float SpeedVip = 227.0f;
constexpr float SpeedShield = 160.0f;
constexpr float SpeedHas_Hostage = 200.0f;
constexpr float SpeedStopped = 1.0f;
constexpr float SpeedObserver = 900.0f;

constexpr float SpeedDuckModifier = 0.34f;
constexpr float SpeedWalkModifier = 0.52f;
constexpr float SpeedClimbModifier = 0.34f;

constexpr float HeavyArmorFlinchModifier = 0.5f;

constexpr float DuckSpeedIdeal = 8.0f;
}
2 changes: 2 additions & 0 deletions Source/Cloud9/Game/Cloud9DeveloperSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ UCloud9DeveloperSettings::UCloud9DeveloperSettings(const FObjectInitializer& Obj
bIsCheatsEnabled = 0;
bIsSelfAimEnabled = 0;
Volume = 0.1;
WeaponAirSpreadScale = 1.0f;
JumpImpulse = 301.993377;
}

UCloud9DeveloperSettings* UCloud9DeveloperSettings::Get()
Expand Down
11 changes: 10 additions & 1 deletion Source/Cloud9/Game/Cloud9DeveloperSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class CLOUD9_API UCloud9DeveloperSettings : public UDeveloperSettings
static FString SelfAimEnabledName;
static FString CameraVerticalSpeedLagName;
static FString VolumeName;

public: // properties
UPROPERTY(config, EditAnywhere, BlueprintReadWrite, Category=Debug)
int32 bIsDrawHitCursorLine;
Expand Down Expand Up @@ -111,6 +111,15 @@ class CLOUD9_API UCloud9DeveloperSettings : public UDeveloperSettings
UPROPERTY(config, EditAnywhere, BlueprintReadWrite, Category=Sound)
float Volume;

UPROPERTY(Config, EditAnywhere, BlueprintReadWrite, Category=Sound)
bool bIsNoSpread;

UPROPERTY(config, EditAnywhere, BlueprintReadWrite, Category=Debug)
float WeaponAirSpreadScale;

UPROPERTY(config, EditAnywhere, BlueprintReadWrite, Category=Debug)
float JumpImpulse;

UPROPERTY(config, EditAnywhere, BlueprintReadWrite, Category=Debug)
EUnUsedEnum UnUsedEnum;

Expand Down
43 changes: 43 additions & 0 deletions Source/Cloud9/Tools/Cloud9ToolsLibrary.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,47 @@ class CLOUD9_API UCloud9ToolsLibrary : public UBlueprintFunctionLibrary

UFUNCTION(BlueprintCallable)
static TArray<FString> GetObjectEditorProperties(UClass* Class);

// see mathlib.h from cstrike15_src

template <typename InputType, typename OutputType>
static constexpr InputType Select(InputType Value, OutputType A, OutputType B)
{
return Value < 0.0f ? A : B;
}

template <typename ValueType>
static constexpr ValueType RemapValue(
ValueType Value,
ValueType InRangeMin,
ValueType InRangeMax,
ValueType OutRangeMin,
ValueType OutRangeMax)
{
if (InRangeMin == InRangeMax)
{
return Select(Value - InRangeMax, OutRangeMax, OutRangeMin);
}

return OutRangeMin + (OutRangeMax - OutRangeMin) * (Value - InRangeMin) / (InRangeMax - InRangeMin);
}

template <typename ValueType>
static constexpr ValueType RemapValueClamped(
ValueType Value,
ValueType InRangeMin,
ValueType InRangeMax,
ValueType OutRangeMin,
ValueType OutRangeMax)
{
if (InRangeMin == InRangeMax)
{
return Select(Value - InRangeMax, OutRangeMax, OutRangeMin);
}

float Temp = (Value - InRangeMin) / (InRangeMax - InRangeMin);
Temp = FMath::Clamp(Temp, 0.0f, 1.0f);

return OutRangeMin + (OutRangeMax - OutRangeMin) * Temp;
}
};
88 changes: 88 additions & 0 deletions Source/Cloud9/Weapon/Classes/Cloud9WeaponFirearm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "Cloud9WeaponFirearm.h"
#include "Engine/StaticMeshActor.h"
#include "NiagaraFunctionLibrary.h"
#include "Cloud9/Cloud9Consts.h"
#include "Components/WidgetInteractionComponent.h"

#include "Cloud9/Tools/Macro/Common.h"
Expand Down Expand Up @@ -101,6 +102,8 @@ bool ACloud9WeaponFirearm::OnInitialize(const FWeaponConfig& WeaponConfig)
CurrentAmmo = WeaponConfig.GetAmmoInMagazine(MaxMagazineSize);
AmmoInReserve = WeaponConfig.GetAmmoInReserve(MaxAmmoInReserve);

AccuracyPenalty = 0.0f;

OnAmmoInMagazineChanged.Broadcast(CurrentAmmo);
OnAmmoInReserveChanged.Broadcast(AmmoInReserve);

Expand Down Expand Up @@ -592,6 +595,91 @@ bool ACloud9WeaponFirearm::UpdateReloadAmmo(bool IsShotgun)
return true;
}

float ACloud9WeaponFirearm::GetInaccuracy()
{
constexpr float MaxFallingPenalty = 2.0f; // Accuracy is never worse than 2x starting penalty
constexpr float MovementCurve01Exponent = 0.25f;

static let Settings = UCloud9DeveloperSettings::Get();

let Character = GetOwner<ACloud9Character>();

if (not IsValid(Character))
{
return 0.0f;
}

if (Settings->bIsNoSpread)
{
return 0.0f;
}

let WeaponInfo = GetWeaponInfo();

float Inaccuracy = AccuracyPenalty;

let Velocity = Character->GetVelocity();

let MaxSpeed = WeaponInfo->GetMaxSpeed();

var MovementInaccuracyScale = UCloud9ToolsLibrary::RemapValueClamped(
Velocity.Size2D(),
MaxSpeed * Cloud9Player::SpeedDuckModifier,
MaxSpeed * 0.95f, // max out at 95% of run speed to avoid jitter near max speed
0.0f,
1.0f);

if (MovementInaccuracyScale > 0.0f)
{
// power curve only applies at speeds greater than walk
if (not Character->bIsSneaking)
{
MovementInaccuracyScale = FMath::Pow(MovementInaccuracyScale, MovementCurve01Exponent);
}

Inaccuracy += MovementInaccuracyScale * WeaponInfo->GetInaccuracyMove();
}

// If we are in the air/on ladder, add inaccuracy based on vertical speed (maximum accuracy at apex of jump)
if (not Character->GetMovementComponent()->IsMovingOnGround())
{
let VerticalSpeed = FMath::Abs(Velocity.Z);

let InaccuracyJumpInitial = WeaponInfo->GetInaccuracyJump() * Settings->WeaponAirSpreadScale;

// Use sqrt here to make the curve more "sudden" around the accurate point at the apex of the jump
let SqrtMaxJumpSpeed = FMath::Sqrt(Settings->JumpImpulse);
let SqrtVerticalSpeed = FMath::Sqrt(VerticalSpeed);

var AirSpeedInaccuracy = UCloud9ToolsLibrary::RemapValue(
SqrtVerticalSpeed,

SqrtMaxJumpSpeed * 0.25f,
// Anything less than 6.25% of maximum speed has no additional accuracy penalty for z-motion (6.25% = .25 * .25)
SqrtMaxJumpSpeed, // Penalty at max jump speed

0.0f, // No movement-related penalty when close to stopped
InaccuracyJumpInitial
); // Movement-penalty at start of jump

// Clamp to min/max values. (Don't use RemapValClamped because it makes clamping to > kJumpMovePenalty hard)
if (AirSpeedInaccuracy < 0.0f)
{
AirSpeedInaccuracy = 0.0f;
}
else if (AirSpeedInaccuracy > MaxFallingPenalty * InaccuracyJumpInitial)
{
AirSpeedInaccuracy = MaxFallingPenalty * InaccuracyJumpInitial;
}

// Apply air velocity inaccuracy penalty
// (There is an additional penalty for being in the air at all applied in UpdateAccuracyPenalty())
Inaccuracy += AirSpeedInaccuracy;
}

return FMath::Min(Inaccuracy, 1.0f);
}

bool ACloud9WeaponFirearm::UpdateMagazineAttachment(bool IsReload)
{
let Character = GetOwner<ACloud9Character>();
Expand Down
4 changes: 4 additions & 0 deletions Source/Cloud9/Weapon/Classes/Cloud9WeaponFirearm.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class CLOUD9_API ACloud9WeaponFirearm : public ACloud9WeaponBase

EFirearmFireStatus Fire(const FFirearmWeaponInfo* WeaponInfo, const FFirearmCommonData& FirearmCommonData);
bool UpdateReloadAmmo(bool IsShotgun);
float GetInaccuracy();

bool UpdateMagazineAttachment(bool IsReload);
void DropMagazine() const;
Expand Down Expand Up @@ -113,6 +114,9 @@ class CLOUD9_API ACloud9WeaponFirearm : public ACloud9WeaponBase

UPROPERTY(Category=Weapon, BlueprintReadOnly, meta=(AllowPrivateAccess))
int MaxAmmoInReserve;

UPROPERTY()
float AccuracyPenalty;
};

template <>
Expand Down
6 changes: 6 additions & 0 deletions Source/Cloud9/Weapon/Tables/WeaponTableFirearm.h
Original file line number Diff line number Diff line change
Expand Up @@ -458,4 +458,10 @@ struct FFirearmWeaponInfo : public FBaseWeaponInfo
UPROPERTY(BlueprintReadOnly, EditDefaultsOnly, Category=Flinch, AdvancedDisplay,
meta=(UIMin="0.0", UIMax="1.0", ClampMin="0", ClampMax="1.0"))
float FlinchVelocityModifierNext = 1.0f;

float GetMaxSpeed(float Scale = 1.0f) const { return MaxPlayerSpeed * Scale; }

float GetInaccuracyMove(float Scale = 0.001f) const { return Inaccuracy.OnMove * Scale; }

float GetInaccuracyJump(float Scale = 0.001f) const { return Inaccuracy.OnJump * Scale; }
};

0 comments on commit 50015c3

Please sign in to comment.