Skip to content

Commit

Permalink
Fix startup errors and restructure init of players
Browse files Browse the repository at this point in the history
  • Loading branch information
Lacyway committed May 21, 2024
1 parent d319cba commit 5c956de
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 39 deletions.
12 changes: 7 additions & 5 deletions Fika.Core/Coop/Components/CoopHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ await Singleton<PoolManager>.Instance.LoadBundlesAndCreatePools(PoolManager.Pool
}
});

LocalPlayer otherPlayer = SpawnObservedPlayer(spawnObject.Profile, spawnObject.Position, playerId, spawnObject.IsAI, spawnObject.NetId);
ObservedCoopPlayer otherPlayer = SpawnObservedPlayer(spawnObject.Profile, spawnObject.Position, playerId, spawnObject.IsAI, spawnObject.NetId);

if (!spawnObject.IsAlive)
{
Expand Down Expand Up @@ -453,9 +453,9 @@ public WorldInteractiveObject GetInteractiveObject(string objectId, out WorldInt
return null;
}

private LocalPlayer SpawnObservedPlayer(Profile profile, Vector3 position, int playerId, bool isAI, int netId)
private ObservedCoopPlayer SpawnObservedPlayer(Profile profile, Vector3 position, int playerId, bool isAI, int netId)
{
LocalPlayer otherPlayer = ObservedCoopPlayer.CreateObservedPlayer(playerId, position, Quaternion.identity,
ObservedCoopPlayer otherPlayer = ObservedCoopPlayer.CreateObservedPlayer(playerId, position, Quaternion.identity,
"Player", isAI == true ? "Bot_" : $"Player_{profile.Nickname}_", EPointOfView.ThirdPerson, profile, isAI,
EUpdateQueue.Update, Player.EUpdateMode.Manual, Player.EUpdateMode.Auto,
GClass549.Config.CharacterController.ObservedPlayerMode,
Expand All @@ -468,7 +468,7 @@ private LocalPlayer SpawnObservedPlayer(Profile profile, Vector3 position, int p
return null;
}

((CoopPlayer)otherPlayer).NetId = netId;
otherPlayer.NetId = netId;
Logger.LogInfo($"SpawnObservedPlayer: {profile.Nickname} spawning with NetId {netId}");
if (!isAI)
{
Expand All @@ -477,7 +477,7 @@ private LocalPlayer SpawnObservedPlayer(Profile profile, Vector3 position, int p

if (!Players.ContainsKey(netId))
{
Players.Add(netId, (CoopPlayer)otherPlayer);
Players.Add(netId, otherPlayer);
}
else
{
Expand Down Expand Up @@ -532,6 +532,8 @@ private LocalPlayer SpawnObservedPlayer(Profile profile, Vector3 position, int p
}
}

otherPlayer.InitObservedPlayer();

Logger.LogDebug($"CreateLocalPlayer::{profile.Info.Nickname}::Spawned.");

SetWeaponInHandsOfNewPlayer(otherPlayer, () => { });
Expand Down
2 changes: 1 addition & 1 deletion Fika.Core/Coop/GameMode/CoopGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ public override async Task<LocalPlayer> vmethod_2(int playerId, Vector3 position
}
else
{
Singleton<FikaClient>.Instance?.SendData(new NetDataWriter(), ref packet, LiteNetLib.DeliveryMethod.ReliableUnordered);
Singleton<FikaClient>.Instance.SendData(new NetDataWriter(), ref packet, LiteNetLib.DeliveryMethod.ReliableUnordered);
}

if (MatchmakerAcceptPatches.IsServer)
Expand Down
2 changes: 1 addition & 1 deletion Fika.Core/Coop/PacketHandlers/ServerPacketSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected void Start()

protected void FixedUpdate()
{
if (player == null || Writer == null)
if (player == null || Writer == null || Server == null)
{
return;
}
Expand Down
11 changes: 3 additions & 8 deletions Fika.Core/Coop/Players/CoopBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public static async Task<LocalPlayer> CreateBot(int playerId, Vector3 position,

InventoryControllerClass inventoryController = new CoopBotInventoryController(player, profile, true);

player.PacketSender = player.gameObject.AddComponent<BotPacketSender>();
player.PacketReceiver = player.gameObject.AddComponent<PacketReceiver>();

await player.Init(rotation, layerName, pointOfView, profile, inventoryController,
new CoopBotHealthController(profile.Health, player, inventoryController, profile.Skills, aiControl),
new CoopObservedStatisticsManager(), null, null, filter,
Expand Down Expand Up @@ -137,14 +140,6 @@ public override GClass1676 ApplyShot(DamageInfo damageInfo, EBodyPart bodyPartTy

protected override void Start()
{
PacketSender = gameObject.AddComponent<BotPacketSender>();
PacketReceiver = gameObject.AddComponent<PacketReceiver>();

/*if (FikaPlugin.DynamicAI.Value)
{
dynamicAi = gameObject.AddComponent<FikaDynamicAI>();
}*/

if (FikaPlugin.DisableBotMetabolism.Value)
{
HealthController.DisableMetabolism();
Expand Down
37 changes: 21 additions & 16 deletions Fika.Core/Coop/Players/CoopPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ public static async Task<LocalPlayer> Create(int playerId, Vector3 position, Qua
achievementsController.Init();
achievementsController.Run();

if (MatchmakerAcceptPatches.IsServer)
{
player.PacketSender = player.gameObject.AddComponent<ServerPacketSender>();
}
else if (MatchmakerAcceptPatches.IsClient)
{
player.PacketSender = player.gameObject.AddComponent<ClientPacketSender>();
}

player.PacketReceiver = player.gameObject.AddComponent<PacketReceiver>();

await player.Init(rotation, layerName, pointOfView, profile, inventoryController,
new CoopClientHealthController(profile.Health, player, inventoryController, profile.Skills, aiControl),
statisticsManager, questController, achievementsController, filter,
Expand Down Expand Up @@ -376,15 +387,18 @@ public override void SendHeadlightsPacket(bool isSilent)
{
GStruct163[] lightStates = _helmetLightControllers.Select(new Func<TacticalComboVisualController, GStruct163>(ClientPlayer.Class1430.class1430_0.method_0)).ToArray();

PacketSender.CommonPlayerPackets.Enqueue(new()
if (PacketSender != null)
{
HasHeadLightsPacket = true,
HeadLightsPacket = new()
PacketSender.CommonPlayerPackets.Enqueue(new()
{
Amount = lightStates.Count(),
LightStates = lightStates
}
});
HasHeadLightsPacket = true,
HeadLightsPacket = new()
{
Amount = lightStates.Count(),
LightStates = lightStates
}
});
}
}

public override void OnItemAddedOrRemoved(Item item, ItemAddress location, bool added)
Expand Down Expand Up @@ -827,15 +841,6 @@ public override void TryInteractionCallback(LootableContainer container)

protected virtual void Start()
{
if (MatchmakerAcceptPatches.IsServer)
{
PacketSender = gameObject.AddComponent<ServerPacketSender>();
}
else if (MatchmakerAcceptPatches.IsClient)
{
PacketSender = gameObject.AddComponent<ClientPacketSender>();
}
PacketReceiver = gameObject.AddComponent<PacketReceiver>();
Profile.Info.GroupId = "Fika";

if (Side != EPlayerSide.Savage)
Expand Down
15 changes: 7 additions & 8 deletions Fika.Core/Coop/Players/ObservedCoopPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public override float SqrCameraDistance
}
#endregion

public static async Task<LocalPlayer> CreateObservedPlayer(int playerId, Vector3 position, Quaternion rotation,
public static async Task<ObservedCoopPlayer> CreateObservedPlayer(int playerId, Vector3 position, Quaternion rotation,
string layerName, string prefix, EPointOfView pointOfView, Profile profile, bool aiControl,
EUpdateQueue updateQueue, EUpdateMode armsUpdateMode, EUpdateMode bodyUpdateMode,
CharacterControllerSpawner.Mode characterControllerMode, Func<float> getSensitivity,
Expand Down Expand Up @@ -890,7 +890,7 @@ private IEnumerator DestroyNetworkedComponents()
}
}

protected override async void Start()
public void InitObservedPlayer()
{
if (gameObject.name.StartsWith("Bot_"))
{
Expand Down Expand Up @@ -954,18 +954,17 @@ protected override async void Start()
EFT.Communications.ENotificationDurationType.Default, EFT.Communications.ENotificationIconType.Friend);
}

// Spawn these later to prevent errors
while (coopGame.Status != GameStatus.Started)
{
await Task.Delay(TimeSpan.FromSeconds(1));
}

healthBar = gameObject.AddComponent<FikaHealthBar>();

RaycastCameraTransform = Traverse.Create(this).Field("_playerLookRaycastTransform").GetValue<Transform>();
}
}

protected override void Start()
{
// Do nothing
}

public override void LateUpdate()
{
DistanceDirty = true;
Expand Down

0 comments on commit 5c956de

Please sign in to comment.