Skip to content

Commit

Permalink
Fix the sync of usage of bullets in guns between clients
Browse files Browse the repository at this point in the history
  • Loading branch information
paulov-t committed May 7, 2024
1 parent 6343f59 commit 96f8bf6
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,10 @@ public override void ReloadWithAmmo(AmmoPack ammoPack, Callback callback)
{
if (CanStartReload())
{
base.ReloadWithAmmo(ammoPack, callback);
StayInTarkov.Coop.NetworkPacket.Player.Weapons.ReloadWithAmmoPacket packet = new (_player.ProfileId, ammoPack.GetReloadingAmmoIds());
GameClient.SendData(packet.Serialize());

base.ReloadWithAmmo(ammoPack, callback);
}
}

Expand Down
38 changes: 29 additions & 9 deletions Source/Coop/NetworkPacket/Player/BasePlayerPacket.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using EFT.InventoryLogic;
using BepInEx.Logging;
using Comfort.Common;
using EFT;
using EFT.InventoryLogic;
using Newtonsoft.Json;
using StayInTarkov.Coop.Components.CoopGameComponents;
using StayInTarkov.Coop.NetworkPacket.Player.Proceed;
Expand All @@ -10,6 +13,13 @@ namespace StayInTarkov.Coop.NetworkPacket.Player
{
public class BasePlayerPacket : BasePacket
{
public static ManualLogSource Logger { get; private set; }

static BasePlayerPacket()
{
Logger = BepInEx.Logging.Logger.CreateLogSource(nameof(BasePlayerPacket));
}

[JsonProperty(PropertyName = "profileId")]
public string ProfileId { get; set; }

Expand Down Expand Up @@ -63,26 +73,36 @@ protected void WriteHeaderAndProfileId(BinaryWriter writer)
writer.Write(ProfileId);
}

//protected void WriteHeaderAndProfileId(NetworkWriter writer)
//{
// WriteHeader(writer);
// writer.Write(ProfileId);
//}

/// <summary>
/// Auto discover Client Player object and Process on them
/// </summary>
public override void Process()
{
//StayInTarkovHelperConstants.Logger.LogDebug($"{GetType()}:{nameof(Process)}:{Method}");

if (!SITGameComponent.TryGetCoopGameComponent(out var coopGameComponent))
{
Logger.LogError("Unable to obtain Game Component");
return;
}

// If it is my Player, it wont be a Client. So ignore.
if (Singleton<GameWorld>.Instantiated && Singleton<GameWorld>.Instance.MainPlayer.ProfileId == ProfileId)
return;

// Find the affected Player
if (coopGameComponent.Players.ContainsKey(ProfileId) && coopGameComponent.Players[ProfileId] is CoopPlayerClient client)
{
Process(client);
}
// This deals with other mods adding players to the world. TODO: Maybe remove the above call?
else if (Singleton<GameWorld>.Instantiated && Singleton<GameWorld>.Instance.allAlivePlayersByID.ContainsKey(ProfileId))
{
if (Singleton<GameWorld>.Instance.allAlivePlayersByID[ProfileId] is CoopPlayerClient client2)
Process(client2);
}
else
{
Logger.LogError($"Unable to find {ProfileId}");
}
}

/// <summary>
Expand Down
19 changes: 10 additions & 9 deletions Source/Coop/NetworkPacket/Player/Weapons/InitiateShotPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
using EFT;
using EFT.InventoryLogic;
using StayInTarkov.Coop.Controllers.HandControllers;
using StayInTarkov.Coop.Matchmaker;
using StayInTarkov.Coop.Players;
using System.Collections;
using System.IO;
using System.Net.Sockets;
using UnityEngine;
using UnityEngine.UIElements;
using static StayInTarkov.Networking.SITSerialization;

namespace StayInTarkov.Coop.NetworkPacket.Player.Weapons
Expand Down Expand Up @@ -138,9 +140,9 @@ protected override void Process(CoopPlayerClient client)
}
break;
case EShotType.RegularShot:

firearmControllerClient.InitiateShot(firearmControllerClient.Weapon, ammoToFire, ShotPosition, ShotDirection, FireportPosition, ChamberIndex, Overheat);
firearmControllerClient.PlaySounds(firearmControllerClient.WeaponSoundPlayer, ammoToFire, ShotPosition, ShotDirection, false);
//firearmControllerClient.FirearmsAnimator.SetFire(fire: true);

if (firearmControllerClient.Weapon.IsBoltCatch && firearmControllerClient.Weapon.ChamberAmmoCount == 0 && firearmControllerClient.Weapon.GetCurrentMagazineCount() == 0 && !firearmControllerClient.Weapon.ManualBoltCatch)
{
Expand Down Expand Up @@ -169,11 +171,6 @@ protected override void Process(CoopPlayerClient client)
firearmControllerClient.LightAndSoundShot(ShotPosition, ShotDirection, ammoToFire.AmmoTemplate);
}

//if (firearmControllerClient.Weapon.HasChambers && firearmControllerClient.Weapon.Chambers[0].ContainedItem != null)
// firearmControllerClient.Weapon.Chambers[0].RemoveItem().OrElse(elseValue: false);

//ammoToFire.IsUsed = true;

break;
default:
break;
Expand All @@ -198,6 +195,9 @@ private void GetBulletToFire(CoopPlayerClient client, Weapon weapon_0, out Bulle
ammoToFire = cylindermag.GetFirstAmmo(singleFireMode: false);
// Is Used?
ammoToFire.IsUsed = true;

Logger.LogDebug($"Used CylinderMagazineClass first bullet {ammoToFire}");

return;
}

Expand All @@ -210,6 +210,7 @@ private void GetBulletToFire(CoopPlayerClient client, Weapon weapon_0, out Bulle
if (ammoToFire != null)
{
Logger.LogDebug($"Used {ammoToFire} in Chamber");
weapon_0.Chambers[0].RemoveItem().OrElse(elseValue: false);
return;
}

Expand All @@ -230,14 +231,14 @@ private void GetBulletToFire(CoopPlayerClient client, Weapon weapon_0, out Bulle
//{
ammoToFire = (BulletClass)currentMagazine.Cartridges.PopToNowhere(pic).Value.Item;

// Logger.LogDebug($"Popped {ammoToFire} to nowhere");
Logger.LogDebug($"Popped {ammoToFire} to nowhere");
//}


}

// Is Used?
ammoToFire.IsUsed = true;
//ammoToFire.IsUsed = true;

if (ammoToFire == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,15 @@ public override ISITPacket Deserialize(byte[] bytes)
return this;
}

public override void Process()
{
StayInTarkovHelperConstants.Logger.LogDebug($"{GetType()}:{nameof(Process)}()");
base.Process();
}

protected override void Process(CoopPlayerClient client)
{
StayInTarkovHelperConstants.Logger.LogDebug($"{GetType()}:{nameof(Process)}");
StayInTarkovHelperConstants.Logger.LogDebug($"{GetType()}:{nameof(Process)}(client)");

List<BulletClass> ammoList = new();
foreach (string ammoId in AmmoIds)
Expand Down

0 comments on commit 96f8bf6

Please sign in to comment.