Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Точность и спад урона #188

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
accuracy system + evasion system [Need test and prototypes]
Lev Kotovskiy committed Dec 19, 2024
commit b4573eddb767c9a4463659ba3e350334a93d8721
43 changes: 43 additions & 0 deletions Content.Shared/_Stalker/Evasion/EvasionComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Content.Shared.FixedPoint;
using Robust.Shared.GameStates;

namespace Content.Shared._Stalker.Evasion;

[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
[Access(typeof(EvasionSystem))]
public sealed partial class EvasionComponent : Component
{
/// <summary>
/// Base evasion value.
/// Conversion from 13: evasion
/// </summary>
[DataField, AutoNetworkedField]
public FixedPoint2 Evasion = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Разъеби лопатой эту хуйню, тут нормально бы смотрелись флоаты


/// <summary>
/// Evasion value after applicable modifiers. This is subtracted from the hit chance of most incoming projectiles.
/// </summary>
[DataField, AutoNetworkedField]
public FixedPoint2 ModifiedEvasion = 0;

/// <summary>
/// Base evasion value for friendly fire.
/// Conversion from 13: FF_hit_evade
/// </summary>
[DataField, AutoNetworkedField]
public FixedPoint2 EvasionFriendly = 15;

/// <summary>
/// Evasion value after applicable modifiers. This is subtracted from the hit chance of most incoming friendly projectiles.
/// </summary>
[DataField, AutoNetworkedField]
public FixedPoint2 ModifiedEvasionFriendly = 0;
}

public enum EvasionModifiers : int
{
Rest = -15,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Чисто RMC хуйня у нас такого нет

Invisibility = 1000,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Кровососы выебут тебя в очко

SizeSmall = 10,
SizeBig = -10
}
10 changes: 10 additions & 0 deletions Content.Shared/_Stalker/Evasion/EvasionRefreshModifiersEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Content.Shared.FixedPoint;

namespace Content.Shared._Stalker.Evasion;

[ByRefEvent]
public record struct EvasionRefreshModifiersEvent(
Entity<EvasionComponent> Entity,
FixedPoint2 Evasion,
FixedPoint2 EvasionFriendly
);
56 changes: 56 additions & 0 deletions Content.Shared/_Stalker/Evasion/EvasionSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Content.Shared._Stalker.Stun;
using Content.Shared.Standing;

namespace Content.Shared._Stalker.Evasion;

public sealed class EvasionSystem : EntitySystem
{
public override void Initialize()
{
SubscribeLocalEvent<EvasionComponent, MapInitEvent>(CallRefresh);
SubscribeLocalEvent<EvasionComponent, DownedEvent>(CallRefresh);
SubscribeLocalEvent<EvasionComponent, StoodEvent>(CallRefresh);

SubscribeLocalEvent<STSizeComponent, EvasionRefreshModifiersEvent>(OnSizeRefreshEvasion);
}

public void RefreshEvasionModifiers(EntityUid entity)
{
if (!TryComp(entity, out EvasionComponent? evasionComponent))
return;

RefreshEvasionModifiers((entity, evasionComponent));
}

public void RefreshEvasionModifiers(Entity<EvasionComponent> entity)
{
var ev = new EvasionRefreshModifiersEvent(
entity,
entity.Comp.Evasion,
entity.Comp.EvasionFriendly);

RaiseLocalEvent(entity.Owner, ref ev);

entity.Comp.ModifiedEvasion = ev.Evasion;
entity.Comp.ModifiedEvasionFriendly = ev.EvasionFriendly;

Dirty(entity);
}

private void CallRefresh<T>(Entity<EvasionComponent> entity, ref T args) where T : notnull
{
RefreshEvasionModifiers(entity);
}

private void OnSizeRefreshEvasion(Entity<STSizeComponent> size, ref EvasionRefreshModifiersEvent args)
{
if (size.Owner != args.Entity.Owner)
return;

if (size.Comp.Size <= STSizes.Small)
args.Evasion += (int) EvasionModifiers.SizeSmall;

if (size.Comp.Size >= STSizes.Big)
args.Evasion += (int) EvasionModifiers.SizeBig;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Robust.Shared.GameStates;

namespace Content.Shared._Stalker.Projectiles;

[RegisterComponent, NetworkedComponent]
[Access(typeof(STProjectileSystem))]
public sealed partial class DeleteOnCollideComponent : Component;
16 changes: 16 additions & 0 deletions Content.Shared/_Stalker/Projectiles/ModifyTargetOnHitComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Content.Shared.Whitelist;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;

namespace Content.Shared._Stalker.Projectiles;

[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
[Access(typeof(STProjectileSystem))]
public sealed partial class ModifyTargetOnHitComponent : Component
{
[DataField]
public ComponentRegistry? Add;

[DataField, AutoNetworkedField]
public EntityWhitelist? Whitelist;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Robust.Shared.GameStates;

namespace Content.Shared._Stalker.Projectiles;

[RegisterComponent, NetworkedComponent]
[Access(typeof(STProjectileSystem))]
public sealed partial class PreventCollideWithDeadComponent : Component;
18 changes: 18 additions & 0 deletions Content.Shared/_Stalker/Projectiles/ProjectileMaxRangeComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Robust.Shared.GameStates;
using Robust.Shared.Map;

namespace Content.Shared._Stalker.Projectiles;

[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
[Access(typeof(STProjectileSystem))]
public sealed partial class ProjectileMaxRangeComponent : Component
{
[DataField, AutoNetworkedField]
public EntityCoordinates? Origin;

[DataField(required: true), AutoNetworkedField]
public float Max;

[DataField, AutoNetworkedField]
public bool Delete = true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using Content.Shared._Stalker.Random;
using Content.Shared._Stalker.Weapons.Ranged;
using Content.Shared.FixedPoint;
using Robust.Shared.GameStates;
using Robust.Shared.Map;
using Robust.Shared.Serialization;

namespace Content.Shared._Stalker.Projectiles;

[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
[Access(typeof(STProjectileSystem), typeof(STGunSystem))]
public sealed partial class STProjectileAccuracyComponent : Component
{
/// <summary>
/// This lists all the thresholds and their falloff values.
/// </summary>
[DataField, AutoNetworkedField]
public List<AccuracyFalloffThreshold> Thresholds = new()
{
new AccuracyFalloffThreshold(5f, 10, false)
};

/// <summary>
/// Minimum hit chance.
/// </summary>
[DataField, AutoNetworkedField]
public FixedPoint2 MinAccuracy = 5;

/// <summary>
/// The accuracy of the projectile before taking into account any modifiers reliant on the target.
/// This value is multiplied by the firing weapon's accuracy multiplier upon the projectile being shot.
/// Conversion from 13: accuracy + accuracy (one from the ammo, the other from the projectile itself)
/// </summary>
[DataField, AutoNetworkedField]
public FixedPoint2 Accuracy = 90;

/// <summary>
/// Setting this to true will make the projectile not apply the usual penalty accuracy penalty when attacking friendlies.
/// </summary>
[DataField, AutoNetworkedField]
public bool IgnoreFriendlyEvasion = false;

/// <summary>
/// If set to true, this makes the projectile automatically hit regardless of accuracy or any other modifiers.
/// </summary>
[DataField, AutoNetworkedField]
public bool ForceHit = false;

/// <summary>
/// These are the coordinates from which the projectile was shot. Used to determine the distance travelled.
/// </summary>
[DataField, AutoNetworkedField]
public EntityCoordinates? ShotFrom;

[DataField, AutoNetworkedField]
public long GunSeed;

[DataField, AutoNetworkedField]
public uint Tick;
}

[DataRecord, Serializable, NetSerializable]
public record struct AccuracyFalloffThreshold(
/// <summary>
/// The range at which accuracy falloff starts to take effect.
/// Conversion from 13: accurate_range
/// Conversion from 13 for buildup: accurate_range_min
/// </summary>
float Range,

/// <summary>
/// This is the number by which the projectile's accuracy is decreased for each tile travelled beyond its effective range.
/// Conversion from 13: 10
/// Conversion from 13 for buildup: accuracy_range_falloff
/// </summary>
FixedPoint2 Falloff,

/// <summary>
/// Setting this to true makes it so AccurateRange is treated as the minimum accurate range.
/// Falloff is applied by how much the shot falls short of that distance, instead of by how much it exceeds it.
/// </summary>
bool Buildup
);

public enum AccuracyModifiers : int
{
TargetOccluded = -15
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Content.Shared.FixedPoint;
using Robust.Shared.GameStates;
using Robust.Shared.Map;
using Robust.Shared.Serialization;

namespace Content.Shared._Stalker.Projectiles;

[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
[Access(typeof(STProjectileSystem))]
public sealed partial class STProjectileDamageFalloffComponent : Component
{
/// <summary>
/// This lists all the thresholds and their falloff values.
/// </summary>
[DataField, AutoNetworkedField]
public List<DamageFalloffThreshold> Thresholds = new()
{
new DamageFalloffThreshold(0f, 1, false),
new DamageFalloffThreshold(22f, 9999, true)
};

/// <summary>
/// This determines the minimum fraction of the projectile's original damage that must remain after falloff is applied.
/// </summary>
[DataField, AutoNetworkedField]
public FixedPoint2 MinRemainingDamageMult = 0.05f;

/// <summary>
/// This is the additional falloff multiplier applied by the firing weapon.
/// </summary>
[DataField, AutoNetworkedField]
public FixedPoint2 WeaponMult = 1;

/// <summary>
/// These are the coordinates from which the projectile was shot. Used to determine the distance travelled.
/// </summary>
[DataField, AutoNetworkedField]
public EntityCoordinates? ShotFrom;
}

[DataRecord, Serializable, NetSerializable]
public record struct DamageFalloffThreshold(
/// <summary>
/// The range at which falloff starts to take effect.
/// Conversion from 13: effective_range_max
/// </summary>
float Range,

/// <summary>
/// This is the number by which the projectile's damage is decreased for each tile travelled beyond its effective range.
/// Conversion from 13: damage_falloff
/// </summary>
FixedPoint2 Falloff,

/// <summary>
/// This makes this falloff value ignore the firing weapon's falloff multiplier. Used primarily to simulate having a capped maximum range. Should generally be false.
/// </summary>
bool IgnoreModifiers
);
263 changes: 263 additions & 0 deletions Content.Shared/_Stalker/Projectiles/STProjectileSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
using System.Numerics;
using Content.Shared._Stalker.Evasion;
using Content.Shared._Stalker.Random;
using Content.Shared.Examine;
using Content.Shared.FixedPoint;
using Content.Shared.Mobs.Systems;
using Content.Shared.NPC.Systems;
using Content.Shared.Popups;
using Content.Shared.Projectiles;
using Content.Shared.Whitelist;
using Robust.Shared.Network;
using Robust.Shared.Physics.Events;
using Robust.Shared.Physics.Systems;
using Robust.Shared.Timing;

namespace Content.Shared._Stalker.Projectiles;

public sealed class STProjectileSystem : EntitySystem
{
[Dependency] private readonly ExamineSystemShared _examine = default!;
[Dependency] private readonly MobStateSystem _mobState = default!;
[Dependency] private readonly NpcFactionSystem _npcFaction = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly INetManager _net = default!;
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;
[Dependency] private readonly EntityWhitelistSystem _whitelist = default!;

public override void Initialize()
{
SubscribeLocalEvent<DeleteOnCollideComponent, StartCollideEvent>(OnDeleteOnCollideStartCollide);
SubscribeLocalEvent<ModifyTargetOnHitComponent, ProjectileHitEvent>(OnModifyTargetOnHit);
SubscribeLocalEvent<ProjectileMaxRangeComponent, MapInitEvent>(OnProjectileMaxRangeMapInit);

SubscribeLocalEvent<STProjectileDamageFalloffComponent, MapInitEvent>(OnFalloffProjectileMapInit);
SubscribeLocalEvent<STProjectileDamageFalloffComponent, ProjectileHitEvent>(OnFalloffProjectileHit);

SubscribeLocalEvent<STProjectileAccuracyComponent, MapInitEvent>(OnProjectileAccuracyMapInit);
SubscribeLocalEvent<STProjectileAccuracyComponent, PreventCollideEvent>(OnProjectileAccuracyPreventCollide);

SubscribeLocalEvent<SpawnOnTerminateComponent, MapInitEvent>(OnSpawnOnTerminatingMapInit);
SubscribeLocalEvent<SpawnOnTerminateComponent, EntityTerminatingEvent>(OnSpawnOnTerminatingTerminate);

SubscribeLocalEvent<PreventCollideWithDeadComponent, PreventCollideEvent>(OnPreventCollideWithDead);
}

private void OnDeleteOnCollideStartCollide(Entity<DeleteOnCollideComponent> ent, ref StartCollideEvent args)
{
if (_net.IsServer)
QueueDel(ent);
}

private void OnModifyTargetOnHit(Entity<ModifyTargetOnHitComponent> ent, ref ProjectileHitEvent args)
{
if (!_whitelist.IsWhitelistPassOrNull(ent.Comp.Whitelist, args.Target))
return;

if (ent.Comp.Add is { } add)
EntityManager.AddComponents(args.Target, add);
}

private void OnProjectileMaxRangeMapInit(Entity<ProjectileMaxRangeComponent> ent, ref MapInitEvent args)
{
ent.Comp.Origin = _transform.GetMoverCoordinates(ent);
Dirty(ent);
}

private void OnFalloffProjectileMapInit(Entity<STProjectileDamageFalloffComponent> projectile, ref MapInitEvent args)
{
projectile.Comp.ShotFrom = _transform.GetMoverCoordinates(projectile.Owner);
Dirty(projectile);
}

private void OnFalloffProjectileHit(Entity<STProjectileDamageFalloffComponent> projectile, ref ProjectileHitEvent args)
{
if (projectile.Comp.ShotFrom == null || projectile.Comp.MinRemainingDamageMult < 0)
return;

var distance = (_transform.GetMoverCoordinates(args.Target).Position - projectile.Comp.ShotFrom.Value.Position).Length();
var minDamage = args.Damage.GetTotal() * projectile.Comp.MinRemainingDamageMult;

foreach (var threshold in projectile.Comp.Thresholds)
{
var pastEffectiveRange = distance - threshold.Range;

if (pastEffectiveRange <= 0)
continue;

var totalDamage = args.Damage.GetTotal();

if (totalDamage <= minDamage)
break;

var extraMult = threshold.IgnoreModifiers ? 1 : projectile.Comp.WeaponMult;
var minMult = FixedPoint2.Min(minDamage / totalDamage, 1);

args.Damage *= FixedPoint2.Clamp((totalDamage - pastEffectiveRange * threshold.Falloff * extraMult) / totalDamage, minMult, 1);

}
}

public void SetProjectileFalloffWeaponMult(Entity<STProjectileDamageFalloffComponent> projectile, FixedPoint2 mult)
{
projectile.Comp.WeaponMult = mult;
Dirty(projectile);
}

private void OnProjectileAccuracyMapInit(Entity<STProjectileAccuracyComponent> projectile, ref MapInitEvent args)
{
projectile.Comp.ShotFrom = _transform.GetMoverCoordinates(projectile.Owner);
projectile.Comp.Tick = _timing.CurTick.Value;

Dirty(projectile);
}

private void OnProjectileAccuracyPreventCollide(Entity<STProjectileAccuracyComponent> projectile, ref PreventCollideEvent args)
{
if (args.Cancelled)
return;

if (projectile.Comp.ForceHit || projectile.Comp.ShotFrom == null)
return;

if (!TryComp(projectile.Owner, out ProjectileComponent? projectileComponent))
return;

if (!TryComp(args.OtherEntity, out EvasionComponent? evasionComponent))
return;

var accuracy = projectile.Comp.Accuracy;
var targetCoords = _transform.GetMoverCoordinates(args.OtherEntity);
var distance = (targetCoords.Position - projectile.Comp.ShotFrom.Value.Position).Length();

foreach (var threshold in projectile.Comp.Thresholds)
{
var pastRange = distance - threshold.Range;

if (threshold.Buildup)
{
if (pastRange >= 0)
continue;

accuracy += threshold.Falloff * pastRange;
continue;
}

if (pastRange <= 0)
continue;

accuracy -= threshold.Falloff * pastRange;
}

if (!_examine.InRangeUnOccluded(_transform.ToMapCoordinates(projectile.Comp.ShotFrom.Value), _transform.ToMapCoordinates(targetCoords), distance, null))
accuracy += (int) AccuracyModifiers.TargetOccluded;

if (!projectile.Comp.IgnoreFriendlyEvasion && IsProjectileTargetFriendly(projectile.Owner, args.OtherEntity))
accuracy -= evasionComponent.ModifiedEvasionFriendly;

accuracy -= evasionComponent.ModifiedEvasion;

accuracy = accuracy > projectile.Comp.MinAccuracy ? accuracy : projectile.Comp.MinAccuracy;

var random = new Xoshiro128P(projectile.Comp.GunSeed, (long) projectile.Comp.Tick << 32 | GetNetEntity(args.OtherEntity).Id).NextFloat(0f, 100f);

if (accuracy >= random)
return;

args.Cancelled = true;
}

private bool IsProjectileTargetFriendly(EntityUid projectile, EntityUid target)
{
if (!TryComp(projectile, out ProjectileComponent? projectileComp) || projectileComp.Shooter == null)
return false;

return _npcFaction.IsEntityFriendly(projectileComp.Shooter.Value, target);
}

private void OnSpawnOnTerminatingMapInit(Entity<SpawnOnTerminateComponent> ent, ref MapInitEvent args)
{
ent.Comp.Origin = _transform.GetMoverCoordinates(ent);
Dirty(ent);
}

private void OnSpawnOnTerminatingTerminate(Entity<SpawnOnTerminateComponent> ent, ref EntityTerminatingEvent args)
{
if (_net.IsClient)
return;

if (!TryComp(ent, out TransformComponent? transform))
return;

if (TerminatingOrDeleted(transform.ParentUid))
return;

var coordinates = transform.Coordinates;
if (ent.Comp.ProjectileAdjust &&
ent.Comp.Origin is { } origin &&
coordinates.TryDelta(EntityManager, _transform, origin, out var delta) &&
delta.Length() > 0)
{
coordinates = coordinates.Offset(delta.Normalized() / -2);
}

SpawnAtPosition(ent.Comp.Spawn, coordinates);

if (ent.Comp.Popup is { } popup)
_popup.PopupCoordinates(Loc.GetString(popup), coordinates, ent.Comp.PopupType ?? PopupType.Small);
}

private void OnPreventCollideWithDead(Entity<PreventCollideWithDeadComponent> ent, ref PreventCollideEvent args)
{
if (args.Cancelled)
return;

if (_mobState.IsDead(args.OtherEntity))
args.Cancelled = true;
}

public void SetMaxRange(Entity<ProjectileMaxRangeComponent> ent, float max)
{
ent.Comp.Max = max;
Dirty(ent);
}

private void StopProjectile(Entity<ProjectileMaxRangeComponent> ent)
{
if (ent.Comp.Delete)
{
if (_net.IsServer)
QueueDel(ent);
}
else
{
_physics.SetLinearVelocity(ent, Vector2.Zero);
RemCompDeferred<ProjectileMaxRangeComponent>(ent);
}
}

public override void Update(float frameTime)
{
if (_net.IsClient)
return;

var maxQuery = EntityQueryEnumerator<ProjectileMaxRangeComponent>();
while (maxQuery.MoveNext(out var uid, out var comp))
{
var coordinates = _transform.GetMoverCoordinates(uid);
if (comp.Origin is not { } origin ||
!coordinates.TryDistance(EntityManager, _transform, origin, out var distance))
{
StopProjectile((uid, comp));
continue;
}

if (distance < comp.Max && Math.Abs(distance - comp.Max) > 0.1f)
continue;

StopProjectile((uid, comp));
}
}
}
26 changes: 26 additions & 0 deletions Content.Shared/_Stalker/Projectiles/SpawnOnTerminateComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Content.Shared.Popups;
using Robust.Shared.GameStates;
using Robust.Shared.Map;
using Robust.Shared.Prototypes;

namespace Content.Shared._Stalker.Projectiles;

[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
[Access(typeof(STProjectileSystem))]
public sealed partial class SpawnOnTerminateComponent : Component
{
[DataField, AutoNetworkedField]
public EntityCoordinates? Origin;

[DataField(required: true), AutoNetworkedField]
public EntProtoId Spawn;

[DataField, AutoNetworkedField]
public LocId? Popup;

[DataField, AutoNetworkedField]
public PopupType? PopupType;

[DataField, AutoNetworkedField]
public bool ProjectileAdjust = true;
}
39 changes: 39 additions & 0 deletions Content.Shared/_Stalker/Random/SplitMix64.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace Content.Shared._Stalker.Random;

/// <summary>
/// Seed initializer PRNG (splitmix64).
/// </summary>
/// <remarks>http://prng.di.unimi.it/splitmix64.c</remarks>
public record struct SplitMix64
{
/// <summary>
/// Creates a new instance.
/// </summary>
public SplitMix64()
: this(DateTime.UtcNow.Ticks)
{
}

/// <summary>
/// Creates a new instance.
/// </summary>
/// <param name="seed">Seed value.</param>
public SplitMix64(long seed)
{
x = (UInt64) seed;
}


private UInt64 x;

/// <summary>
/// Returns the next 64-bit pseudo-random number.
/// </summary>
public long Next()
{
UInt64 z = unchecked(x += 0x9e3779b97f4a7c15);
z = unchecked((z ^ (z >> 30)) * 0xbf58476d1ce4e5b9);
z = unchecked((z ^ (z >> 27)) * 0x94d049bb133111eb);
return unchecked((Int64) (z ^ (z >> 31)));
}
}
84 changes: 84 additions & 0 deletions Content.Shared/_Stalker/Random/Xoshiro128P.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System.Runtime.CompilerServices;

namespace Content.Shared._Stalker.Random;

// https://github.com/medo64/Medo.ScrambledLinear/blob/main/src/Xoshiro/Xoshiro128P.cs
/// <summary>
/// 32-bit generator intended for floating point numbers with 128-bit state (xoshiro128+).
/// </summary>
/// <remarks>http://prng.di.unimi.it/xoshiro128plus.c</remarks>
public record struct Xoshiro128P
{
private UInt32 _s0;
private UInt32 _s1;
private UInt32 _s2;
private UInt32 _s3;


/// <summary>
/// Creates a new instance.
/// </summary>
public Xoshiro128P() : this(DateTime.UtcNow.Ticks) { }

/// <summary>
/// Creates a new instance.
/// </summary>
/// <param name="seed">Seed value.</param>
public Xoshiro128P(long seed)
{
var sm64 = new SplitMix64(seed);

_s0 = unchecked((UInt32)sm64.Next());
_s1 = unchecked((UInt32)sm64.Next());
_s2 = unchecked((UInt32)sm64.Next());
_s3 = unchecked((UInt32)sm64.Next());
}

public Xoshiro128P(long s0, long s1)
{
var sm64 = new SplitMix64(s0);
_s0 = unchecked((UInt32)sm64.Next());
_s1 = unchecked((UInt32)sm64.Next());

sm64 = new SplitMix64(s1);
_s2 = unchecked((UInt32)sm64.Next());
_s3 = unchecked((UInt32)sm64.Next());
}

/// <summary>
/// Returns the next 32-bit pseudo-random number.
/// </summary>
public int Next()
{
UInt32 result = unchecked(_s0 + _s3);

UInt32 t = _s1 << 9;

_s2 ^= _s0;
_s3 ^= _s1;
_s1 ^= _s2;
_s0 ^= _s3;

_s2 ^= t;

_s3 = RotateLeft(_s3, 11);

return Math.Abs((int)result);
}

public float NextFloat()
{
return Next() * 4.6566128752458E-10f;
}

public float NextFloat(float min, float max)
{
return NextFloat() * (max - min) + min;
}

private static UInt32 RotateLeft(UInt32 x, int k)
{
return (x << k) | (x >> (32 - k));
}

}
24 changes: 24 additions & 0 deletions Content.Shared/_Stalker/Stun/RMCSizeStunSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace Content.Shared._Stalker.Stun;

public sealed class STSizeStunSystem : EntitySystem
{
public bool IsHumanoidSized(Entity<STSizeComponent> ent)
{
return ent.Comp.Size <= STSizes.Humanoid;
}

public bool IsXenoSized(Entity<STSizeComponent> ent)
{
return ent.Comp.Size >= STSizes.VerySmallXeno;
}

public bool TryGetSize(EntityUid ent, out STSizes size)
{
size = default;
if (!TryComp(ent, out STSizeComponent? sizeComp))
return false;

size = sizeComp.Size;
return true;
}
}
24 changes: 24 additions & 0 deletions Content.Shared/_Stalker/Stun/STSizeComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;

namespace Content.Shared._Stalker.Stun;

[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
public sealed partial class STSizeComponent : Component
{
[DataField(required: true), AutoNetworkedField]
public STSizes Size = STSizes.Humanoid;
}


[Serializable, NetSerializable]
public enum STSizes : byte
{
Small,
Humanoid,
VerySmallXeno,
SmallXeno,
Xeno,
Big,
Immobile
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Content.Shared.FixedPoint;

namespace Content.Shared._Stalker.Weapons.Ranged;

[ByRefEvent]
public record struct GetDamageFalloffEvent(
FixedPoint2 FalloffMultiplier
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Content.Shared.FixedPoint;

namespace Content.Shared._Stalker.Weapons.Ranged;

[ByRefEvent]
public record struct GetWeaponAccuracyEvent(
FixedPoint2 AccuracyMultiplier
);
94 changes: 94 additions & 0 deletions Content.Shared/_Stalker/Weapons/Ranged/STGunSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using Content.Shared._Stalker.Projectiles;
using Content.Shared.Examine;
using Content.Shared.FixedPoint;
using Content.Shared.Weapons.Ranged.Components;
using Content.Shared.Weapons.Ranged.Events;
using Content.Shared.Wieldable.Components;

namespace Content.Shared._Stalker.Weapons.Ranged;

public sealed class STGunSystem : EntitySystem
{
[Dependency] private readonly STProjectileSystem _stProjectileSystem = default!;

private const string AccuracyExamineColour = "yellow";

public override void Initialize()
{
SubscribeLocalEvent<STWeaponDamageFalloffComponent, AmmoShotEvent>(OnWeaponDamageFalloffShot);
SubscribeLocalEvent<STWeaponDamageFalloffComponent, GunRefreshModifiersEvent>(OnWeaponDamageFalloffRefreshModifiers);

SubscribeLocalEvent<STWeaponAccuracyComponent, ExaminedEvent>(OnWeaponAccuracyExamined);
SubscribeLocalEvent<STWeaponAccuracyComponent, GunRefreshModifiersEvent>(OnWeaponAccuracyRefreshModifiers);
SubscribeLocalEvent<STWeaponAccuracyComponent, AmmoShotEvent>(OnWeaponAccuracyShot);
}

private void OnWeaponDamageFalloffRefreshModifiers(Entity<STWeaponDamageFalloffComponent> weapon, ref GunRefreshModifiersEvent args)
{
var ev = new GetDamageFalloffEvent(weapon.Comp.FalloffMultiplier);
RaiseLocalEvent(weapon.Owner, ref ev);

weapon.Comp.ModifiedFalloffMultiplier = FixedPoint2.Max(ev.FalloffMultiplier, 0);

Dirty(weapon);
}

private void OnWeaponDamageFalloffShot(Entity<STWeaponDamageFalloffComponent> weapon, ref AmmoShotEvent args)
{
foreach (var projectile in args.FiredProjectiles)
{
if (!TryComp(projectile, out STProjectileDamageFalloffComponent? falloffComponent))
continue;

_stProjectileSystem.SetProjectileFalloffWeaponMult((projectile, falloffComponent), weapon.Comp.ModifiedFalloffMultiplier);
}
}

private void OnWeaponAccuracyExamined(Entity<STWeaponAccuracyComponent> weapon, ref ExaminedEvent args)
{
if (!HasComp<GunComponent>(weapon.Owner))
return;

using (args.PushGroup(nameof(STWeaponAccuracyComponent)))
{
args.PushMarkup(Loc.GetString("st-examine-text-weapon-accuracy", ("colour", AccuracyExamineColour), ("accuracy", weapon.Comp.ModifiedAccuracyMultiplier)));
}
}

private void OnWeaponAccuracyRefreshModifiers(Entity<STWeaponAccuracyComponent> weapon, ref GunRefreshModifiersEvent args)
{
var baseMult = weapon.Comp.AccuracyMultiplierUnwielded;

if (TryComp(weapon.Owner, out WieldableComponent? wieldableComponent) && wieldableComponent.Wielded)
baseMult = weapon.Comp.AccuracyMultiplier;

var ev = new GetWeaponAccuracyEvent(baseMult);
RaiseLocalEvent(weapon.Owner, ref ev);

weapon.Comp.ModifiedAccuracyMultiplier = Math.Max(0.1, (double) ev.AccuracyMultiplier);

Dirty(weapon);
}

private void OnWeaponAccuracyShot(Entity<STWeaponAccuracyComponent> weapon, ref AmmoShotEvent args)
{
var netId = GetNetEntity(weapon.Owner).Id;
FixedPoint2 orderAccuracy = 0;
FixedPoint2 orderAccuracyPerTile = 0;

for (int t = 0; t < args.FiredProjectiles.Count; ++t)
{
if (!TryComp(args.FiredProjectiles[t], out STProjectileAccuracyComponent? accuracyComponent))
continue;

accuracyComponent.Accuracy *= weapon.Comp.ModifiedAccuracyMultiplier;
accuracyComponent.Accuracy += orderAccuracy;

if (orderAccuracyPerTile != 0)
accuracyComponent.Thresholds.Add(new AccuracyFalloffThreshold(0f, -orderAccuracyPerTile, false));

accuracyComponent.GunSeed = (long) t << 32 | netId;
Dirty<STProjectileAccuracyComponent>((args.FiredProjectiles[t], accuracyComponent));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Content.Shared.FixedPoint;
using Robust.Shared.GameStates;

namespace Content.Shared._Stalker.Weapons.Ranged;

[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
[Access(typeof(STGunSystem))]
public sealed partial class STWeaponAccuracyComponent : Component
{
/// <summary>
/// This is the base multiplier applied to all fired projectiles' accuracy scores when the weapon is wielded.
/// Conversion from 13: accuracy_mult
/// </summary>
[DataField, AutoNetworkedField]
public FixedPoint2 AccuracyMultiplier = 1;

/// <summary>
/// This is the base multiplier applied to all fired projectiles' accuracy scores when the weapon is not wielded.
/// Conversion from 13: accuracy_mult_unwielded
/// </summary>
[DataField, AutoNetworkedField]
public FixedPoint2 AccuracyMultiplierUnwielded = 1;

[DataField, AutoNetworkedField]
public FixedPoint2 ModifiedAccuracyMultiplier = 1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Content.Shared.FixedPoint;
using Robust.Shared.GameStates;

namespace Content.Shared._Stalker.Weapons.Ranged;

[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
[Access(typeof(STGunSystem))]
public sealed partial class STWeaponDamageFalloffComponent : Component
{
/// <summary>
/// This is the baase multiplier applied the all fired projectiles' falloff.
/// Conversion from 13: damage_falloff_mult
/// </summary>
[DataField, AutoNetworkedField]
public FixedPoint2 FalloffMultiplier = 1;

[DataField, AutoNetworkedField]
public FixedPoint2 ModifiedFalloffMultiplier = 1;
}