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

Make "Armored Train" appear for every clients at the same time #116

Merged
merged 1 commit into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
38 changes: 38 additions & 0 deletions Source/Coop/Components/ActionPacketHandlerComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using BepInEx.Logging;
using Comfort.Common;
using EFT;
using EFT.MovingPlatforms;
using EFT.UI.BattleTimer;
using StayInTarkov.AkiSupport.Airdrops.Models;
using StayInTarkov.Coop.Matchmaker;
Expand Down Expand Up @@ -220,6 +221,10 @@ bool ProcessWorldPacket(ref Dictionary<string, object> packet)
ReplicateTimeAndWeather(packet);
result = true;
break;
case "ArmoredTrainTime":
ReplicateArmoredTrainTime(packet);
result = true;
break;
case "LootableContainer_Interact":
LootableContainer_Interact_Patch.Replicated(packet);
result = true;
Expand Down Expand Up @@ -462,5 +467,38 @@ static bool VectorIsSameQuadrant(Vector2 v1, Vector2 v2, out int flag)
}
}
}

void ReplicateArmoredTrainTime(Dictionary<string, object> packet)
{
CoopGameComponent coopGameComponent = CoopGameComponent.GetCoopGameComponent();
if (coopGameComponent == null)
return;

if (MatchmakerAcceptPatches.IsClient)
{
DateTime utcTime = new(long.Parse(packet["utcTime"].ToString()));

if (coopGameComponent.LocalGameInstance is CoopGame coopGame)
{
Timer1 gameTimer = coopGame.GameTimer;

// Process only after raid began.
if (gameTimer.StartDateTime.HasValue && gameTimer.SessionTime.HasValue)
{
// Looking for Armored Train, if there is nothing, then we are not on the Reserve or Lighthouse.
Locomotive locomotive = FindObjectOfType<Locomotive>();
if (locomotive != null)
{
// The time won't change, if we already have replicated the time, don't override it again.
FieldInfo departField = ReflectionHelpers.GetFieldFromType(typeof(MovingPlatform), "_depart");
if (utcTime == (DateTime)departField.GetValue(locomotive))
return;

locomotive.Init(utcTime);
}
}
}
}
}
}
}
38 changes: 37 additions & 1 deletion Source/Coop/CoopGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using EFT.Game.Spawning;
using EFT.InputSystem;
using EFT.Interactive;
using EFT.MovingPlatforms;
using EFT.UI;
using EFT.Weather;
using JsonType;
Expand All @@ -19,6 +20,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using UnityEngine;

Expand Down Expand Up @@ -200,6 +202,7 @@ public void CreateCoopGameComponent()
StartCoroutine(HostPinger());
StartCoroutine(GameTimerSync());
StartCoroutine(TimeAndWeatherSync());
StartCoroutine(ArmoredTrainTimeSync());
}

StartCoroutine(ClientLoadingPinger());
Expand Down Expand Up @@ -289,7 +292,7 @@ private IEnumerator GameTimerSync()
}
}

public IEnumerator TimeAndWeatherSync()
private IEnumerator TimeAndWeatherSync()
{
var waitSeconds = new WaitForSeconds(15f);

Expand Down Expand Up @@ -335,6 +338,39 @@ public IEnumerator TimeAndWeatherSync()
}
}

private IEnumerator ArmoredTrainTimeSync()
{
var waitSeconds = new WaitForSeconds(30f);

while (true)
{
yield return waitSeconds;

if (!CoopGameComponent.TryGetCoopGameComponent(out var coopGameComponent))
yield break;

// Make sure packet is only sent after the raid begins.
if (GameTimer.StartDateTime.HasValue && GameTimer.SessionTime.HasValue)
{
// Looking for Armored Train, if there is nothing, then we are not on the Reserve or Lighthouse.
Locomotive locomotive = FindObjectOfType<Locomotive>();
if (locomotive == null)
yield break;

// Utc time where the train will start moving.
FieldInfo departField = ReflectionHelpers.GetFieldFromType(typeof(MovingPlatform), "_depart");

Dictionary<string, object> dict = new()
{
{ "serverId", coopGameComponent.ServerId },
{ "m", "ArmoredTrainTime" },
{ "utcTime", ((DateTime)departField.GetValue(locomotive)).Ticks },
};
AkiBackendCommunication.Instance.SendDataToPool(dict.ToJson());
}
}
}

public Dictionary<string, EFT.Player> Bots { get; } = new ();

private async Task<LocalPlayer> CreatePhysicalBot(Profile profile, Vector3 position)
Expand Down