Skip to content

Commit

Permalink
Separate bot and client healthcontrollers
Browse files Browse the repository at this point in the history
- Improved bot spawn coroutine as well
  • Loading branch information
Lacyway committed May 15, 2024
1 parent c59750e commit fb02520
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 30 deletions.
36 changes: 36 additions & 0 deletions Fika.Core/Coop/BotClasses/CoopBotHealthController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// © 2024 Lacyway All Rights Reserved

using EFT;
using EFT.HealthSystem;
using EFT.UI;
using Fika.Core.Coop.Players;

namespace Fika.Core.Coop.ClientClasses
{
public sealed class CoopBotHealthController(Profile.GClass1756 healthInfo, Player player, InventoryControllerClass inventoryController, SkillManager skillManager, bool aiHealth)
: PlayerHealthController(healthInfo, player, inventoryController, skillManager, aiHealth)
{
private readonly CoopBot coopBot = (CoopBot)player;
public override bool _sendNetworkSyncPackets
{
get
{
return true;
}
}

public override void SendNetworkSyncPacket(GStruct346 packet)
{
if (packet.SyncType == GStruct346.ESyncType.IsAlive && !packet.Data.IsAlive.IsAlive)
{
coopBot?.PacketSender?.HealthSyncPackets.Enqueue(coopBot.SetupDeathPacket(packet));
return;
}

coopBot?.PacketSender?.HealthSyncPackets.Enqueue(new(coopBot.NetId)
{
Packet = packet
});
}
}
}
27 changes: 4 additions & 23 deletions Fika.Core/Coop/ClientClasses/CoopClientHealthController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ public sealed class CoopClientHealthController(Profile.GClass1756 healthInfo, Pl
: PlayerHealthController(healthInfo, player, inventoryController, skillManager, aiHealth)
{
private readonly CoopPlayer coopPlayer = (CoopPlayer)player;
private readonly bool isBot = aiHealth;
private readonly CoopBot coopBot = aiHealth ? (CoopBot)player : null;
public override bool _sendNetworkSyncPackets
{
get
Expand All @@ -24,31 +22,14 @@ public override void SendNetworkSyncPacket(GStruct346 packet)
{
if (packet.SyncType == GStruct346.ESyncType.IsAlive && !packet.Data.IsAlive.IsAlive)
{
if (!isBot)
{
coopPlayer?.PacketSender?.HealthSyncPackets.Enqueue(coopPlayer.SetupDeathPacket(packet));
}
else
{
coopBot?.PacketSender?.HealthSyncPackets.Enqueue(coopBot.SetupDeathPacket(packet));
}
coopPlayer?.PacketSender?.HealthSyncPackets.Enqueue(coopPlayer.SetupDeathPacket(packet));
return;
}

if (!isBot)
coopPlayer?.PacketSender?.HealthSyncPackets.Enqueue(new(coopPlayer.NetId)
{
coopPlayer?.PacketSender?.HealthSyncPackets.Enqueue(new(coopPlayer.NetId)
{
Packet = packet
});
}
else
{
coopBot?.PacketSender?.HealthSyncPackets.Enqueue(new(coopBot.NetId)
{
Packet = packet
});
}
Packet = packet
});
}
}
}
26 changes: 19 additions & 7 deletions Fika.Core/Coop/Players/CoopBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static async Task<LocalPlayer> CreateBot(
InventoryControllerClass inventoryController = new CoopBotInventoryController(player, profile, true);

await player.Init(rotation, layerName, pointOfView, profile, inventoryController,
new CoopClientHealthController(profile.Health, player, inventoryController, profile.Skills, aiControl),
new CoopBotHealthController(profile.Health, player, inventoryController, profile.Skills, aiControl),
new CoopObservedStatisticsManager(), questController, achievementController, filter,
EVoipState.NotAvailable, aiControl, async: false);

Expand Down Expand Up @@ -219,23 +219,35 @@ private IEnumerator WaitForPlayersToLoadBot()
Vector3 spawnPosition = Position;
int connectedPeers = Singleton<FikaServer>.Instance.NetServer.ConnectedPeersCount;
float randomY = UnityEngine.Random.RandomRange(-500, -1000);
DateTime start = DateTime.Now;
float start = 0f;
Teleport(new(0, randomY, 0));

while (loadedPlayers < connectedPeers && Math.Abs((start - DateTime.Now).TotalSeconds) < 30)
while (loadedPlayers < connectedPeers && start < 30f)
{
yield return new WaitForSeconds(1);
start += Time.deltaTime;
yield return new WaitForEndOfFrame();
}

Teleport(new Vector3(spawnPosition.x, spawnPosition.y + 1f, spawnPosition.z));
AIData.BotOwner.BotState = EBotState.PreActive;
isStarted = true;
DateTime fallStart = DateTime.Now;
yield return new WaitUntil(() => { return MovementContext.IsGrounded || Math.Abs((fallStart - DateTime.Now).TotalSeconds) < 2; });
float fallStart = 0f;

while (!MovementContext.IsGrounded || fallStart < 5f)
{
fallStart += Time.deltaTime;
yield return new WaitForEndOfFrame();
}

if (!MovementContext.IsGrounded)
{
FikaPlugin.Instance.FikaLogger.LogWarning($"{gameObject.name} was still not grounded after being loaded in! Forcing to be grounded.");
MovementContext.IsGrounded = true;
}
#if DEBUG
FikaPlugin.Instance.FikaLogger.LogWarning($"{gameObject.name} is now grounded, started at Y: {randomY}, now at {Position.ToStringVerbose()}");
#endif
yield return new WaitForFixedUpdate();
yield return new WaitForEndOfFrame();
ActiveHealthController.SetDamageCoeff(originalDamageCoeff);
}

Expand Down

0 comments on commit fb02520

Please sign in to comment.