Skip to content

Commit

Permalink
Ragdoll sync
Browse files Browse the repository at this point in the history
  • Loading branch information
Lacyway committed Dec 16, 2024
1 parent 6c33f8d commit aa3fba9
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 1 deletion.
71 changes: 71 additions & 0 deletions Fika.Core/Coop/Components/CorpsePositionSyncer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using Comfort.Common;
using EFT.Interactive;
using Fika.Core.Networking;
using Fika.Core.Networking.Packets;
using HarmonyLib;
using LiteNetLib;
using System.Reflection;
using UnityEngine;

namespace Fika.Core.Coop.Components
{
internal class CorpsePositionSyncer : MonoBehaviour
{
private FieldInfo ragdollDoneField = AccessTools.Field(typeof(RagdollClass), "bool_2");

private FikaServer server;
private Corpse corpse;
private GStruct129 data;

public static void Create(GameObject gameObject, Corpse corpse)
{
CorpsePositionSyncer corpsePositionSyncer = gameObject.AddComponent<CorpsePositionSyncer>();
corpsePositionSyncer.corpse = corpse;
corpsePositionSyncer.server = Singleton<FikaServer>.Instance;
corpsePositionSyncer.data = new()
{
Id = corpse.GetNetId()
};
}

public void Start()
{
if (corpse == null)
{
FikaPlugin.Instance.FikaLogger.LogError("CorpsePositionSyncer::Start: Corpse was null!");
Destroy(this);
}

if (!corpse.HasRagdoll)
{
FikaPlugin.Instance.FikaLogger.LogError("CorpsePositionSyncer::Start: Ragdoll was null!");
Destroy(this);
}
}

public void FixedUpdate()
{
if ((bool)ragdollDoneField.GetValue(corpse.Ragdoll))
{
data.Position = corpse.TrackableTransform.position;
data.TransformSyncs = corpse.TransformSyncs;
data.Done = true;
CorpsePositionPacket endPacket = new()
{
Data = data
};
server.SendDataToAll(ref endPacket, DeliveryMethod.ReliableOrdered);
Destroy(this);
return;
}

data.Position = corpse.TrackableTransform.position;
CorpsePositionPacket packet = new()
{
Data = data
};

server.SendDataToAll(ref packet, DeliveryMethod.Unreliable);
}
}
}
8 changes: 8 additions & 0 deletions Fika.Core/Coop/Players/CoopPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using EFT.Vehicle;
using Fika.Core.Coop.ClientClasses;
using Fika.Core.Coop.ClientClasses.HandsControllers;
using Fika.Core.Coop.Components;
using Fika.Core.Coop.HostClasses;
using Fika.Core.Coop.ObservedClasses.Snapshotting;
using Fika.Core.Coop.PacketHandlers;
Expand Down Expand Up @@ -626,6 +627,13 @@ public override void OnPhraseTold(EPhraseTrigger @event, TaggedClip clip, TagBan
}
}

public override Corpse CreateCorpse()
{
Corpse corpse = base.CreateCorpse();
CorpsePositionSyncer.Create(corpse.gameObject, corpse);
return corpse;
}

public override void OperateStationaryWeapon(StationaryWeapon stationaryWeapon, GStruct177.EStationaryCommand command)
{
base.OperateStationaryWeapon(stationaryWeapon, command);
Expand Down
12 changes: 11 additions & 1 deletion Fika.Core/Coop/Players/ObservedCoopPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using EFT.Interactive;
using EFT.InventoryLogic;
using EFT.Vaulting;
using Fika.Core.Coop.Components;
using Fika.Core.Coop.Custom;
using Fika.Core.Coop.Factories;
using Fika.Core.Coop.GameMode;
Expand Down Expand Up @@ -794,7 +795,16 @@ public override Corpse CreateCorpse()
{
SetInventory(CorpseSyncPacket.InventoryDescriptor);
}
return CreateCorpse<Corpse>(CorpseSyncPacket.OverallVelocity);
if (FikaBackendUtils.IsClient)
{
ObservedCorpse observedCorpse = CreateCorpse<ObservedCorpse>(CorpseSyncPacket.OverallVelocity);
Singleton<GameWorld>.Instance.ObservedPlayersCorpses.Add(observedCorpse.GetNetId(), observedCorpse);
return observedCorpse;
}

Corpse corpse = CreateCorpse<Corpse>(CorpseSyncPacket.OverallVelocity);
CorpsePositionSyncer.Create(corpse.gameObject, corpse);
return corpse;
}

public override void OnDead(EDamageType damageType)
Expand Down
16 changes: 16 additions & 0 deletions Fika.Core/Networking/FikaClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
using Fika.Core.Coop.Utils;
using Fika.Core.Modding;
using Fika.Core.Modding.Events;
using Fika.Core.Networking.Packets;
using Fika.Core.Utils;
using FlyingWormConsole3;
using HarmonyLib;
using LiteNetLib;
using LiteNetLib.Utils;
Expand Down Expand Up @@ -158,6 +160,7 @@ public async void Init()
packetProcessor.SubscribeNetSerializable<PingPacket>(OnPingPacketReceived);
packetProcessor.SubscribeNetSerializable<LootSyncPacket>(OnLootSyncPacketReceived);
packetProcessor.SubscribeNetSerializable<LoadingProfilePacket>(OnLoadingProfilePacketReceived);
packetProcessor.SubscribeNetSerializable<CorpsePositionPacket>(OnCorpsePositionPacketReceived);

#if DEBUG
AddDebugPackets();
Expand Down Expand Up @@ -209,6 +212,19 @@ public async void Init()
FikaEventDispatcher.DispatchEvent(new FikaNetworkManagerCreatedEvent(this));
}

private void OnCorpsePositionPacketReceived(CorpsePositionPacket packet)
{
GameWorld gameWorld = Singleton<GameWorld>.Instance;
if (gameWorld != null)
{
if (gameWorld.ObservedPlayersCorpses.TryGetValue(packet.Data.Id, out ObservedCorpse corpse))
{
corpse.ApplyNetPacket(packet.Data);
return;
}
}
}

private void OnLoadingProfilePacketReceived(LoadingProfilePacket packet)
{
if (packet.Profiles != null)
Expand Down
49 changes: 49 additions & 0 deletions Fika.Core/Networking/Packets/GameWorld/CorpsePositionPacket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using LiteNetLib.Utils;

namespace Fika.Core.Networking
{
public struct CorpsePositionPacket : INetSerializable
{
public GStruct129 Data;

public void Deserialize(NetDataReader reader)
{
Data = new()
{
Id = reader.GetInt(),
Position = reader.GetVector3(),
Done = reader.GetBool()
};

if (Data.Done)
{
Data.TransformSyncs = new GStruct107[12];
for (int i = 0; i < 12; i++)
{
Data.TransformSyncs[i] = new()
{
Position = reader.GetVector3(),
Rotation = reader.GetQuaternion()
};
}
}
}

public void Serialize(NetDataWriter writer)
{
writer.Put(Data.Id);
writer.Put(Data.Position);
writer.Put(Data.Done);

if (Data.Done && Data.TransformSyncs != null)
{
GStruct107[] transforms = Data.TransformSyncs;
for (int i = 0; i < 12; i++)
{
writer.Put(transforms[i].Position);
writer.Put(transforms[i].Rotation);
}
}
}
}
}

0 comments on commit aa3fba9

Please sign in to comment.