Skip to content

Commit

Permalink
Fix minefields and sniper zones not causing damage
Browse files Browse the repository at this point in the history
  • Loading branch information
Lacyway committed May 16, 2024
1 parent 0d9c4a2 commit 441b561
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 27 deletions.
8 changes: 4 additions & 4 deletions Fika.Core/Coop/PacketHandlers/BotPacketSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class BotPacketSender : MonoBehaviour, IPacketSender
public FikaClient Client { get; set; }
public NetDataWriter Writer { get; set; } = new();
public Queue<WeaponPacket> FirearmPackets { get; set; } = new(50);
public Queue<DamagePacket> HealthPackets { get; set; } = new(50);
public Queue<DamagePacket> DamagePackets { get; set; } = new(50);
public Queue<InventoryPacket> InventoryPackets { get; set; } = new(50);
public Queue<CommonPlayerPacket> CommonPlayerPackets { get; set; } = new(50);
public Queue<HealthSyncPacket> HealthSyncPackets { get; set; } = new(50);
Expand Down Expand Up @@ -62,12 +62,12 @@ private void Update()
Server?.SendDataToAll(Writer, ref firearmPacket, DeliveryMethod.ReliableOrdered);
}
}
int healthPackets = HealthPackets.Count;
int healthPackets = DamagePackets.Count;
if (healthPackets > 0)
{
for (int i = 0; i < healthPackets; i++)
{
DamagePacket healthPacket = HealthPackets.Dequeue();
DamagePacket healthPacket = DamagePackets.Dequeue();
healthPacket.NetId = player.NetId;

Writer?.Reset();
Expand Down Expand Up @@ -116,7 +116,7 @@ public void DestroyThis()
{
Writer = null;
FirearmPackets.Clear();
HealthPackets.Clear();
DamagePackets.Clear();
InventoryPackets.Clear();
CommonPlayerPackets.Clear();
HealthSyncPackets.Clear();
Expand Down
8 changes: 4 additions & 4 deletions Fika.Core/Coop/PacketHandlers/ClientPacketSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ClientPacketSender : MonoBehaviour, IPacketSender
public FikaClient Client { get; set; }
public NetDataWriter Writer { get; set; } = new();
public Queue<WeaponPacket> FirearmPackets { get; set; } = new(50);
public Queue<DamagePacket> HealthPackets { get; set; } = new(50);
public Queue<DamagePacket> DamagePackets { get; set; } = new(50);
public Queue<InventoryPacket> InventoryPackets { get; set; } = new(50);
public Queue<CommonPlayerPacket> CommonPlayerPackets { get; set; } = new(50);
public Queue<HealthSyncPacket> HealthSyncPackets { get; set; } = new(50);
Expand Down Expand Up @@ -74,12 +74,12 @@ private void Update()
Client?.SendData(Writer, ref firearmPacket, DeliveryMethod.ReliableOrdered);
}
}
int healthPackets = HealthPackets.Count;
int healthPackets = DamagePackets.Count;
if (healthPackets > 0)
{
for (int i = 0; i < healthPackets; i++)
{
DamagePacket healthPacket = HealthPackets.Dequeue();
DamagePacket healthPacket = DamagePackets.Dequeue();
healthPacket.NetId = player.NetId;

Writer?.Reset();
Expand Down Expand Up @@ -199,7 +199,7 @@ public void DestroyThis()
{
Writer = null;
FirearmPackets.Clear();
HealthPackets.Clear();
DamagePackets.Clear();
InventoryPackets.Clear();
CommonPlayerPackets.Clear();
HealthSyncPackets.Clear();
Expand Down
2 changes: 1 addition & 1 deletion Fika.Core/Coop/PacketHandlers/IPacketSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface IPacketSender
public FikaClient Client { get; set; }
public NetDataWriter Writer { get; set; }
public Queue<WeaponPacket> FirearmPackets { get; set; }
public Queue<DamagePacket> HealthPackets { get; set; }
public Queue<DamagePacket> DamagePackets { get; set; }
public Queue<InventoryPacket> InventoryPackets { get; set; }
public Queue<CommonPlayerPacket> CommonPlayerPackets { get; set; }
public Queue<HealthSyncPacket> HealthSyncPackets { get; set; }
Expand Down
14 changes: 7 additions & 7 deletions Fika.Core/Coop/PacketHandlers/ObservedPacketSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ObservedPacketSender : MonoBehaviour, IPacketSender
public FikaClient Client { get; set; }
public NetDataWriter Writer { get; set; } = new();
public Queue<WeaponPacket> FirearmPackets { get; set; } = new(50);
public Queue<DamagePacket> HealthPackets { get; set; } = new(50);
public Queue<DamagePacket> DamagePackets { get; set; } = new(50);
public Queue<InventoryPacket> InventoryPackets { get; set; } = new(50);
public Queue<CommonPlayerPacket> CommonPlayerPackets { get; set; } = new(50);
public Queue<HealthSyncPacket> HealthSyncPackets { get; set; } = new(50);
Expand All @@ -45,14 +45,14 @@ private void Update()
return;
}

if (HealthPackets.Count > 0)
if (DamagePackets.Count > 0)
{
if (isServer)
{
int healthPackets = HealthPackets.Count;
int healthPackets = DamagePackets.Count;
for (int i = 0; i < healthPackets; i++)
{
DamagePacket healthPacket = HealthPackets.Dequeue();
DamagePacket healthPacket = DamagePackets.Dequeue();
healthPacket.NetId = player.NetId;

Writer.Reset();
Expand All @@ -61,10 +61,10 @@ private void Update()
}
else
{
int healthPackets = HealthPackets.Count;
int healthPackets = DamagePackets.Count;
for (int i = 0; i < healthPackets; i++)
{
DamagePacket healthPacket = HealthPackets.Dequeue();
DamagePacket healthPacket = DamagePackets.Dequeue();
healthPacket.NetId = player.NetId;

Writer.Reset();
Expand All @@ -78,7 +78,7 @@ public void DestroyThis()
{
Writer = null;
FirearmPackets.Clear();
HealthPackets.Clear();
DamagePackets.Clear();
InventoryPackets.Clear();
CommonPlayerPackets.Clear();
HealthSyncPackets.Clear();
Expand Down
8 changes: 4 additions & 4 deletions Fika.Core/Coop/PacketHandlers/ServerPacketSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class ServerPacketSender : MonoBehaviour, IPacketSender
public FikaClient Client { get; set; }
public NetDataWriter Writer { get; set; } = new();
public Queue<WeaponPacket> FirearmPackets { get; set; } = new(50);
public Queue<DamagePacket> HealthPackets { get; set; } = new(50);
public Queue<DamagePacket> DamagePackets { get; set; } = new(50);
public Queue<InventoryPacket> InventoryPackets { get; set; } = new(50);
public Queue<CommonPlayerPacket> CommonPlayerPackets { get; set; } = new(50);
public Queue<HealthSyncPacket> HealthSyncPackets { get; set; } = new(50);
Expand Down Expand Up @@ -81,12 +81,12 @@ private void Update()
Server?.SendDataToAll(Writer, ref firearmPacket, DeliveryMethod.ReliableOrdered);
}
}
int healthPackets = HealthPackets.Count;
int healthPackets = DamagePackets.Count;
if (healthPackets > 0)
{
for (int i = 0; i < healthPackets; i++)
{
DamagePacket healthPacket = HealthPackets.Dequeue();
DamagePacket healthPacket = DamagePackets.Dequeue();
healthPacket.NetId = player.NetId;

Writer?.Reset();
Expand Down Expand Up @@ -195,7 +195,7 @@ public void DestroyThis()
{
Writer = null;
FirearmPackets.Clear();
HealthPackets.Clear();
DamagePackets.Clear();
InventoryPackets.Clear();
CommonPlayerPackets.Clear();
HealthSyncPackets.Clear();
Expand Down
78 changes: 77 additions & 1 deletion Fika.Core/Coop/Patches/Minefield/Minefield_method_2_Patch.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
using Aki.Reflection.Patching;
using Comfort.Common;
using EFT;
using EFT.Interactive;
using Fika.Core.Coop.Matchmaker;
using Fika.Core.Coop.Players;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
using static UnityEngine.ParticleSystem;
using static UnityEngine.Tilemaps.Tile;

namespace Fika.Core.Coop.Patches
{
Expand All @@ -17,13 +25,81 @@ protected override MethodBase GetTargetMethod()
}

[PatchPrefix]
public static bool Prefix(IPlayer player)
public static bool Prefix(IPlayer player, bool first, List<IPlayer> ___TargetedPlayers,
List<IPlayer> ___NotTargetedPlayers, float ____collateralContusionRange, float ____collateralDamageRange,
float ____firstExplosionDamage, float ____secondExplosionDamage, Minefield __instance)
{
if (player is ObservedCoopPlayer)
{
if (MatchmakerAcceptPatches.IsServer)
{
Vector3 position = player.Position;
foreach (IPlayer player2 in ___TargetedPlayers.Concat(___NotTargetedPlayers).ToList())
{
DoReplicatedMineDamage(player2, Vector3.Distance(position, player2.Position), first,
player != player2, ____collateralContusionRange, ____collateralDamageRange,
____firstExplosionDamage, ____secondExplosionDamage, __instance);
}
}
return false;
}
return true;
}

private static void DoReplicatedMineDamage(IPlayer player, float distance, bool first, bool isCollateral,
float collateralContusionRange, float collateralDamageRange, float firstExplosionDamage,
float secondExplosionDamage, Minefield minefield)
{
if (isCollateral && distance > collateralContusionRange)
{
return;
}

CoopPlayer coopPlayer = (CoopPlayer)Singleton<GameWorld>.Instance.GetAlivePlayerByProfileID(player.ProfileId);
if (isCollateral && distance > collateralDamageRange)
{
return;
}

if (coopPlayer != null)
{
float num2 = 1f - distance / collateralDamageRange;
IEnumerable<BodyPartCollider> enumerable = isCollateral ? player.PlayerBones.BodyPartColliders.Where(new Func<BodyPartCollider, bool>(minefield.method_4))
: player.PlayerBones.BodyPartColliders.Where(new Func<BodyPartCollider, bool>(minefield.method_5));

enumerable = enumerable.DistinctBy(new Func<BodyPartCollider, EBodyPart>(Minefield.Class2286.class2286_0.method_0)).ToArray();
enumerable = enumerable.Randomize();

int num3 = ((isCollateral || first) ? UnityEngine.Random.Range(2, enumerable.Count()) : int.MaxValue);
float num4 = (isCollateral || first) ? firstExplosionDamage : secondExplosionDamage;
int num5 = 0;

foreach (BodyPartCollider bodyPartCollider in enumerable)
{
coopPlayer.PacketSender?.DamagePackets?.Enqueue(new()
{
DamageInfo = new()
{
DamageType = EDamageType.Landmine,
Damage = num4 * num2,
ArmorDamage = 0.5f,
PenetrationPower = 30f,
Direction = Vector3.zero,
HitNormal = Vector3.zero,
ColliderType = bodyPartCollider.BodyPartColliderType,
BodyPartType = bodyPartCollider.BodyPartType
}
});
if (++num5 >= num3)
{
break;
}
}
}
else
{
FikaPlugin.Instance.FikaLogger.LogError($"DoReplicatedMineDamage: Could not find player with ProfileId: {player.ProfileId}, Nickname: {player.Profile.Nickname}!");
}
}
}
}
10 changes: 8 additions & 2 deletions Fika.Core/Coop/Players/CoopPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,12 @@ public override void ApplyDamageInfo(DamageInfo damageInfo, EBodyPart bodyPartTy

public override GClass1676 ApplyShot(DamageInfo damageInfo, EBodyPart bodyPartType, EBodyPartColliderType colliderType, EArmorPlateCollider armorPlateCollider, GStruct390 shotId)
{
if (damageInfo.Player != null & damageInfo.Player.iPlayer is CoopBot)
if (damageInfo.DamageType is EDamageType.Sniper or EDamageType.Landmine)
{
return base.ApplyShot(damageInfo, bodyPartType, colliderType, armorPlateCollider, shotId);
}

if (damageInfo.Player != null && damageInfo.Player.iPlayer is CoopBot)
{
return base.ApplyShot(damageInfo, bodyPartType, colliderType, armorPlateCollider, shotId);
}
Expand Down Expand Up @@ -1333,7 +1338,8 @@ public virtual void HandleDamagePacket(DamagePacket packet)
PenetrationPower = packet.DamageInfo.PenetrationPower,
BlockedBy = packet.DamageInfo.BlockedBy,
DeflectedBy = packet.DamageInfo.DeflectedBy,
SourceId = packet.DamageInfo.SourceId
SourceId = packet.DamageInfo.SourceId,
ArmorDamage = packet.DamageInfo.ArmorDamage
};

if (!string.IsNullOrEmpty(packet.DamageInfo.ProfileId))
Expand Down
58 changes: 54 additions & 4 deletions Fika.Core/Coop/Players/ObservedCoopPlayer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// © 2024 Lacyway All Rights Reserved

using Aki.SinglePlayer.Patches.MainMenu;
using Comfort.Common;
using EFT;
using EFT.Ballistics;
Expand All @@ -11,6 +12,7 @@
using Fika.Core.Coop.Custom;
using Fika.Core.Coop.Factories;
using Fika.Core.Coop.GameMode;
using Fika.Core.Coop.Matchmaker;
using Fika.Core.Coop.ObservedClasses;
using Fika.Core.Coop.PacketHandlers;
using Fika.Core.Networking;
Expand Down Expand Up @@ -278,6 +280,31 @@ public override bool ShouldVocalizeDeath(EBodyPart bodyPart)

public override void ApplyDamageInfo(DamageInfo damageInfo, EBodyPart bodyPartType, EBodyPartColliderType colliderType, float absorbed)
{
if (damageInfo.DamageType == EDamageType.Landmine && MatchmakerAcceptPatches.IsServer)
{
PacketSender?.DamagePackets?.Enqueue(new()
{
DamageInfo = new()
{
Damage = damageInfo.Damage,
DamageType = damageInfo.DamageType,
BodyPartType = bodyPartType,
ColliderType = colliderType,
Absorbed = 0f,
Direction = damageInfo.Direction,
Point = damageInfo.HitPoint,
HitNormal = damageInfo.HitNormal,
PenetrationPower = damageInfo.PenetrationPower,
BlockedBy = damageInfo.BlockedBy,
DeflectedBy = damageInfo.DeflectedBy,
SourceId = damageInfo.SourceId,
ArmorDamage = damageInfo.ArmorDamage
}
});

return;
}

if (damageInfo.Player == null)
{
return;
Expand Down Expand Up @@ -324,11 +351,32 @@ public override void ApplyDamageInfo(DamageInfo damageInfo, EBodyPart bodyPartTy

public override GClass1676 ApplyShot(DamageInfo damageInfo, EBodyPart bodyPartType, EBodyPartColliderType colliderType, EArmorPlateCollider armorPlateCollider, GStruct390 shotId)
{
/*if (!IsObservedAI)
if (damageInfo.DamageType == EDamageType.Sniper && MatchmakerAcceptPatches.IsServer)
{
ShotReactions(damageInfo, bodyPartType);
PacketSender?.DamagePackets?.Enqueue(new()
{
DamageInfo = new()
{
Damage = damageInfo.Damage,
DamageType = damageInfo.DamageType,
BodyPartType = bodyPartType,
ColliderType = colliderType,
ArmorPlateCollider = armorPlateCollider,
Absorbed = 0f,
Direction = damageInfo.Direction,
Point = damageInfo.HitPoint,
HitNormal = damageInfo.HitNormal,
PenetrationPower = damageInfo.PenetrationPower,
BlockedBy = damageInfo.BlockedBy,
DeflectedBy = damageInfo.DeflectedBy,
SourceId = damageInfo.SourceId,
ArmorDamage = damageInfo.ArmorDamage
}
});

return null;
}*/
}

if (damageInfo.Player != null)
{
Expand Down Expand Up @@ -374,7 +422,7 @@ public override GClass1676 ApplyShot(DamageInfo damageInfo, EBodyPart bodyPartTy
colliderType = bodyPartCollider.BodyPartColliderType;
}

PacketSender?.HealthPackets?.Enqueue(new()
PacketSender?.DamagePackets?.Enqueue(new()
{
DamageInfo = new()
{
Expand All @@ -391,6 +439,7 @@ public override GClass1676 ApplyShot(DamageInfo damageInfo, EBodyPart bodyPartTy
BlockedBy = damageInfo.BlockedBy,
DeflectedBy = damageInfo.DeflectedBy,
SourceId = damageInfo.SourceId,
ArmorDamage = damageInfo.ArmorDamage,
ProfileId = damageInfo.Player.iPlayer.ProfileId
}
});
Expand Down Expand Up @@ -433,7 +482,7 @@ public override GClass1676 ApplyShot(DamageInfo damageInfo, EBodyPart bodyPartTy
colliderType = bodyPartCollider.BodyPartColliderType;
}

PacketSender?.HealthPackets?.Enqueue(new()
PacketSender?.DamagePackets?.Enqueue(new()
{
DamageInfo = new()
{
Expand All @@ -450,6 +499,7 @@ public override GClass1676 ApplyShot(DamageInfo damageInfo, EBodyPart bodyPartTy
BlockedBy = damageInfo.BlockedBy,
DeflectedBy = damageInfo.DeflectedBy,
SourceId = damageInfo.SourceId,
ArmorDamage = damageInfo.ArmorDamage,
ProfileId = damageInfo.Player.iPlayer.ProfileId
}
});
Expand Down
Loading

0 comments on commit 441b561

Please sign in to comment.