Skip to content

Commit

Permalink
Rework damage and armor damage
Browse files Browse the repository at this point in the history
- Damage now sends damage after handling through armor
- Armor damage is now synced to everyone else

This should provide near live-like, if not more accurate, accuracy
  • Loading branch information
Lacyway committed Jun 14, 2024
1 parent f14cce8 commit 6d0e831
Show file tree
Hide file tree
Showing 13 changed files with 257 additions and 153 deletions.
25 changes: 19 additions & 6 deletions Fika.Core/Coop/PacketHandlers/BotPacketSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class BotPacketSender : MonoBehaviour, IPacketSender
public NetDataWriter Writer { get; set; } = new();
public Queue<WeaponPacket> FirearmPackets { get; set; } = new(50);
public Queue<DamagePacket> DamagePackets { get; set; } = new(50);
public Queue<ArmorDamagePacket> ArmorDamagePackets { 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 @@ -87,16 +88,28 @@ protected void Update()
Server.SendDataToAll(Writer, ref firearmPacket, DeliveryMethod.ReliableOrdered);
}
}
int healthPackets = DamagePackets.Count;
if (healthPackets > 0)
int damagePackets = DamagePackets.Count;
if (damagePackets > 0)
{
for (int i = 0; i < healthPackets; i++)
for (int i = 0; i < damagePackets; i++)
{
DamagePacket healthPacket = DamagePackets.Dequeue();
healthPacket.NetId = player.NetId;
DamagePacket damagePacket = DamagePackets.Dequeue();
damagePacket.NetId = player.NetId;

Writer.Reset();
Server.SendDataToAll(Writer, ref healthPacket, DeliveryMethod.ReliableOrdered);
Server.SendDataToAll(Writer, ref damagePacket, DeliveryMethod.ReliableOrdered);
}
}
int armorDamagePackets = ArmorDamagePackets.Count;
if (armorDamagePackets > 0)
{
for (int i = 0; i < armorDamagePackets; i++)
{
ArmorDamagePacket armorDamagePacket = ArmorDamagePackets.Dequeue();
armorDamagePacket.NetId = player.NetId;

Writer.Reset();
Server.SendDataToAll(Writer, ref armorDamagePacket, DeliveryMethod.ReliableOrdered);
}
}
int inventoryPackets = InventoryPackets.Count;
Expand Down
25 changes: 19 additions & 6 deletions Fika.Core/Coop/PacketHandlers/ClientPacketSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class ClientPacketSender : MonoBehaviour, IPacketSender
public NetDataWriter Writer { get; set; } = new();
public Queue<WeaponPacket> FirearmPackets { get; set; } = new(50);
public Queue<DamagePacket> DamagePackets { get; set; } = new(50);
public Queue<ArmorDamagePacket> ArmorDamagePackets { 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 @@ -95,16 +96,28 @@ protected void Update()
Client.SendData(Writer, ref firearmPacket, DeliveryMethod.ReliableOrdered);
}
}
int healthPackets = DamagePackets.Count;
if (healthPackets > 0)
int damagePackets = DamagePackets.Count;
if (damagePackets > 0)
{
for (int i = 0; i < healthPackets; i++)
for (int i = 0; i < damagePackets; i++)
{
DamagePacket healthPacket = DamagePackets.Dequeue();
healthPacket.NetId = player.NetId;
DamagePacket damagePacket = DamagePackets.Dequeue();
damagePacket.NetId = player.NetId;

Writer.Reset();
Client.SendData(Writer, ref healthPacket, DeliveryMethod.ReliableOrdered);
Client.SendData(Writer, ref damagePacket, DeliveryMethod.ReliableOrdered);
}
}
int armorDamagePackets = ArmorDamagePackets.Count;
if (armorDamagePackets > 0)
{
for (int i = 0; i < armorDamagePackets; i++)
{
ArmorDamagePacket armorDamagePacket = ArmorDamagePackets.Dequeue();
armorDamagePacket.NetId = player.NetId;

Writer.Reset();
Client.SendData(Writer, ref armorDamagePacket, DeliveryMethod.ReliableOrdered);
}
}
int inventoryPackets = InventoryPackets.Count;
Expand Down
1 change: 1 addition & 0 deletions Fika.Core/Coop/PacketHandlers/IPacketSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public interface IPacketSender
public NetDataWriter Writer { get; set; }
public Queue<WeaponPacket> FirearmPackets { get; set; }
public Queue<DamagePacket> DamagePackets { get; set; }
public Queue<ArmorDamagePacket> ArmorDamagePackets { get; set; }
public Queue<InventoryPacket> InventoryPackets { get; set; }
public Queue<CommonPlayerPacket> CommonPlayerPackets { get; set; }
public Queue<HealthSyncPacket> HealthSyncPackets { get; set; }
Expand Down
39 changes: 29 additions & 10 deletions Fika.Core/Coop/PacketHandlers/ObservedPacketSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class ObservedPacketSender : MonoBehaviour, IPacketSender
public NetDataWriter Writer { get; set; } = new();
public Queue<WeaponPacket> FirearmPackets { get; set; } = new(50);
public Queue<DamagePacket> DamagePackets { get; set; } = new(50);
public Queue<ArmorDamagePacket> ArmorDamagePackets { 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 @@ -66,26 +67,44 @@ protected void Update()
{
if (isServer)
{
int healthPackets = DamagePackets.Count;
for (int i = 0; i < healthPackets; i++)
int damagePackets = DamagePackets.Count;
for (int i = 0; i < damagePackets; i++)
{
DamagePacket healthPacket = DamagePackets.Dequeue();
healthPacket.NetId = player.NetId;
DamagePacket damagePacket = DamagePackets.Dequeue();
damagePacket.NetId = player.NetId;

Writer.Reset();
Server.SendDataToAll(Writer, ref healthPacket, DeliveryMethod.ReliableOrdered);
Server.SendDataToAll(Writer, ref damagePacket, DeliveryMethod.ReliableOrdered);
}
int armorDamagePackets = ArmorDamagePackets.Count;
for (int i = 0; i < armorDamagePackets; i++)
{
ArmorDamagePacket armorDamagePacket = ArmorDamagePackets.Dequeue();
armorDamagePacket.NetId = player.NetId;

Writer.Reset();
Server.SendDataToAll(Writer, ref armorDamagePacket, DeliveryMethod.ReliableOrdered);
}
}
else
{
int healthPackets = DamagePackets.Count;
for (int i = 0; i < healthPackets; i++)
int damagePackets = DamagePackets.Count;
for (int i = 0; i < damagePackets; i++)
{
DamagePacket damagePacket = DamagePackets.Dequeue();
damagePacket.NetId = player.NetId;

Writer.Reset();
Client.SendData(Writer, ref damagePacket, DeliveryMethod.ReliableOrdered);
}
int armorDamagePackets = ArmorDamagePackets.Count;
for (int i = 0; i < armorDamagePackets; i++)
{
DamagePacket healthPacket = DamagePackets.Dequeue();
healthPacket.NetId = player.NetId;
ArmorDamagePacket armorDamagePacket = ArmorDamagePackets.Dequeue();
armorDamagePacket.NetId = player.NetId;

Writer.Reset();
Client.SendData(Writer, ref healthPacket, DeliveryMethod.ReliableOrdered);
Client.SendData(Writer, ref armorDamagePacket, DeliveryMethod.ReliableOrdered);
}
}
}
Expand Down
20 changes: 15 additions & 5 deletions Fika.Core/Coop/PacketHandlers/PacketReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class PacketReceiver : MonoBehaviour
public PlayerStatePacket NewState { get; set; }
public Queue<WeaponPacket> FirearmPackets { get; private set; } = new(50);
public Queue<DamagePacket> DamagePackets { get; private set; } = new(50);
public Queue<ArmorDamagePacket> ArmorDamagePackets { get; private set; } = new(50);
public Queue<InventoryPacket> InventoryPackets { get; private set; } = new(50);
public Queue<CommonPlayerPacket> CommonPlayerPackets { get; private set; } = new(50);
public Queue<HealthSyncPacket> HealthSyncPackets { get; private set; } = new(50);
Expand Down Expand Up @@ -99,13 +100,22 @@ protected void Update()
player.HandleWeaponPacket(FirearmPackets.Dequeue());
}
}
int healthPackets = DamagePackets.Count;
if (healthPackets > 0)
int damagePackets = DamagePackets.Count;
if (damagePackets > 0)
{
for (int i = 0; i < healthPackets; i++)
for (int i = 0; i < damagePackets; i++)
{
DamagePacket packet = DamagePackets.Dequeue();
player.HandleDamagePacket(ref packet);
DamagePacket damagePacket = DamagePackets.Dequeue();
player.HandleDamagePacket(ref damagePacket);
}
}
int armorDamagePackets = ArmorDamagePackets.Count;
if (armorDamagePackets > 0)
{
for (int i = 0; i < armorDamagePackets; i++)
{
ArmorDamagePacket armorDamagePacket = ArmorDamagePackets.Dequeue();
player.HandleArmorDamagePacket(ref armorDamagePacket);
}
}
int inventoryPackets = InventoryPackets.Count;
Expand Down
25 changes: 19 additions & 6 deletions Fika.Core/Coop/PacketHandlers/ServerPacketSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class ServerPacketSender : MonoBehaviour, IPacketSender
public NetDataWriter Writer { get; set; } = new();
public Queue<WeaponPacket> FirearmPackets { get; set; } = new(50);
public Queue<DamagePacket> DamagePackets { get; set; } = new(50);
public Queue<ArmorDamagePacket> ArmorDamagePackets { 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 @@ -99,16 +100,28 @@ protected void Update()
Server.SendDataToAll(Writer, ref firearmPacket, DeliveryMethod.ReliableOrdered);
}
}
int healthPackets = DamagePackets.Count;
if (healthPackets > 0)
int damagePackets = DamagePackets.Count;
if (damagePackets > 0)
{
for (int i = 0; i < healthPackets; i++)
for (int i = 0; i < damagePackets; i++)
{
DamagePacket healthPacket = DamagePackets.Dequeue();
healthPacket.NetId = player.NetId;
DamagePacket damagePacket = DamagePackets.Dequeue();
damagePacket.NetId = player.NetId;

Writer.Reset();
Server.SendDataToAll(Writer, ref healthPacket, DeliveryMethod.ReliableOrdered);
Server.SendDataToAll(Writer, ref damagePacket, DeliveryMethod.ReliableOrdered);
}
}
int armorDamagePackets = ArmorDamagePackets.Count;
if (armorDamagePackets > 0)
{
for (int i = 0; i < armorDamagePackets; i++)
{
ArmorDamagePacket armorDamagePacket = ArmorDamagePackets.Dequeue();
armorDamagePacket.NetId = player.NetId;

Writer.Reset();
Server.SendDataToAll(Writer, ref armorDamagePacket, DeliveryMethod.ReliableOrdered);
}
}
int inventoryPackets = InventoryPackets.Count;
Expand Down
14 changes: 6 additions & 8 deletions Fika.Core/Coop/Players/CoopBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,6 @@ public override void CreateMovementContext()
MovementContext = BotMovementContext.Create(this, new Func<IAnimator>(GetBodyAnimatorCommon), new Func<ICharacterController>(GetCharacterControllerCommon), movement_MASK);
}

/*public override void ApplyDamageInfo(DamageInfo damageInfo, EBodyPart bodyPartType, EBodyPartColliderType colliderType, float absorbed)
{
if (damageInfo.Player != null && damageInfo.Player.iPlayer is ObservedCoopPlayer)
return;
base.ApplyDamageInfo(damageInfo, bodyPartType, colliderType, absorbed);
}*/

public override GClass1688 ApplyShot(DamageInfo damageInfo, EBodyPart bodyPartType, EBodyPartColliderType colliderType, EArmorPlateCollider armorPlateCollider, GStruct389 shotId)
{
if (damageInfo.Player != null && damageInfo.Player.iPlayer is ObservedCoopPlayer)
Expand Down Expand Up @@ -136,6 +128,12 @@ public override GClass1688 ApplyShot(DamageInfo damageInfo, EBodyPart bodyPartTy
ApplyDamageInfo(damageInfo, bodyPartType, colliderType, 0f);
ShotReactions(damageInfo, bodyPartType);
ReceiveDamage(damageInfo.Damage, bodyPartType, damageInfo.DamageType, num, hitInfo.Material);

if (list != null)
{
QueueArmorDamagePackets([.. list]);
}

return hitInfo;
}

Expand Down
Loading

0 comments on commit 6d0e831

Please sign in to comment.