Skip to content

Commit

Permalink
Merge pull request #45 from project-fika/dev
Browse files Browse the repository at this point in the history
Dev > Main
  • Loading branch information
Lacyway authored May 15, 2024
2 parents 544ac8a + a53f1ef commit ce9f007
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected override MethodBase GetTargetMethod()

[PatchPostfix]
private static void PatchPostfix(RaidSettingsWindow __instance, UiElementBlocker ____coopModeBlocker, List<CanvasGroup> ____weatherCanvasGroups,
UpdatableToggle ____randomTimeToggle, UpdatableToggle ____randomWeatherToggle, List<CanvasGroup> ____waterAndFoodCanvasGroups)
UpdatableToggle ____randomTimeToggle, UpdatableToggle ____randomWeatherToggle, List<CanvasGroup> ____waterAndFoodCanvasGroups, List<CanvasGroup> ____playersSpawnPlaceCanvasGroups, DropDownBox ____playersSpawnPlaceDropdown)
{
// Always disable the Coop Mode checkbox
____coopModeBlocker.SetBlock(true, "Co-op is always enabled in Fika");
Expand All @@ -36,6 +36,26 @@ private static void PatchPostfix(RaidSettingsWindow __instance, UiElementBlocker
canvasGroup.SetUnlockStatus(true, true);
}

foreach (CanvasGroup canvasGroup in ____playersSpawnPlaceCanvasGroups)
{
canvasGroup.SetUnlockStatus(true, true);
}

// Remove redundant settings and add our own "Random" to make the setting clear, while also renaming index 0 to "Together"
List<BaseDropDownBox.Struct945> labelList = Traverse.Create(____playersSpawnPlaceDropdown).Field("list_0").GetValue<List<BaseDropDownBox.Struct945>>();
labelList.Clear();
labelList.Add(new()
{
Label = "Together",
Enabled = true
});
labelList.Add(new()
{
Label = "Random",
Enabled = true
});
____playersSpawnPlaceDropdown.SetTextInternal("Together");

instance = __instance;

____randomWeatherToggle.Bind(new Action<bool>(ToggleWeather));
Expand Down
35 changes: 35 additions & 0 deletions Fika.Core/Coop/BotClasses/CoopBotHealthController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// © 2024 Lacyway All Rights Reserved

using EFT;
using EFT.HealthSystem;
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
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,8 @@ public override void ShotMisfired(BulletClass ammo, Weapon.EMalfunctionState mal
IsPrimaryActive = true,
ShotType = shotType,
AmmoAfterShot = Item.GetCurrentMagazineCount(),
Overheat = overheat
Overheat = overheat,
AmmoTemplate = ammo.TemplateId
}
});

Expand Down
4 changes: 3 additions & 1 deletion Fika.Core/Coop/Components/CoopHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ public static string GetServerId()
{
CoopHandler coopGC = GetCoopHandler();
if (coopGC == null)
return null;
{
return MatchmakerAcceptPatches.GetGroupId();
}

return coopGC.ServerId;
}
Expand Down
52 changes: 36 additions & 16 deletions Fika.Core/Coop/GameMode/CoopGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public async Task CreateCoopHandler()

private List<CoopPlayer> GetPlayers(CoopHandler coopHandler)
{
List<CoopPlayer> humanPlayers = new List<CoopPlayer>();
List<CoopPlayer> humanPlayers = new();

// Grab all players
foreach (CoopPlayer player in coopHandler.Players.Values)
Expand Down Expand Up @@ -498,7 +498,7 @@ private async void DeployScreen(float timeBeforeDeploy)
Destroy(fikaStartButton);
}

SetStatusModel status = new SetStatusModel(coopHandler.MyPlayer.ProfileId, LobbyEntry.ELobbyStatus.IN_GAME);
SetStatusModel status = new(coopHandler.MyPlayer.ProfileId, LobbyEntry.ELobbyStatus.IN_GAME);
await FikaRequestHandler.UpdateSetStatus(status);

Singleton<FikaServer>.Instance.ReadyClients++;
Expand All @@ -515,7 +515,7 @@ private async void DeployScreen(float timeBeforeDeploy)

if (MatchmakerAcceptPatches.IsServer)
{
SetStatusModel status = new SetStatusModel(coopHandler.MyPlayer.ProfileId, LobbyEntry.ELobbyStatus.IN_GAME);
SetStatusModel status = new(coopHandler.MyPlayer.ProfileId, LobbyEntry.ELobbyStatus.IN_GAME);
await FikaRequestHandler.UpdateSetStatus(status);

do
Expand Down Expand Up @@ -581,26 +581,44 @@ private async Task SendOrReceiveSpawnPoint()
{
if (MatchmakerAcceptPatches.IsServer)
{
UpdateSpawnPointRequest body = new UpdateSpawnPointRequest(spawnPoint.Id);
Logger.LogInfo($"Setting Spawn Point to: {spawnPoint.Id}");
bool spawnTogether = RaidSettings.PlayersSpawnPlace == EPlayersSpawnPlace.SamePlace;
UpdateSpawnPointRequest body = new(spawnTogether ? spawnPoint.Id : "");
if (spawnTogether)
{
Logger.LogInfo($"Setting Spawn Point to: {spawnPoint.Id}");
}
else
{
Logger.LogInfo("Using random spawn points!");
NotificationManagerClass.DisplayMessageNotification("Using random spawn points", iconType: EFT.Communications.ENotificationIconType.Alert);
}
await FikaRequestHandler.UpdateSpawnPoint(body);
}
else if (MatchmakerAcceptPatches.IsClient)
{
SpawnPointRequest body = new SpawnPointRequest();
SpawnPointRequest body = new();
SpawnPointResponse response = await FikaRequestHandler.RaidSpawnPoint(body);
string name = response.SpawnPoint;

Logger.LogInfo($"Retrieved Spawn Point '{name}' from server");

Dictionary<ISpawnPoint, SpawnPointMarker> allSpawnPoints = Traverse.Create(spawnPoints).Field("dictionary_0").GetValue<Dictionary<ISpawnPoint, SpawnPointMarker>>();
foreach (ISpawnPoint spawnPointObject in allSpawnPoints.Keys)
if (!string.IsNullOrEmpty(name))
{
if (spawnPointObject.Id == name)
Logger.LogInfo($"Retrieved Spawn Point '{name}' from server");

Dictionary<ISpawnPoint, SpawnPointMarker> allSpawnPoints = Traverse.Create(spawnPoints).Field("dictionary_0").GetValue<Dictionary<ISpawnPoint, SpawnPointMarker>>();
foreach (ISpawnPoint spawnPointObject in allSpawnPoints.Keys)
{
spawnPoint = spawnPointObject;
if (spawnPointObject.Id == name)
{
spawnPoint = spawnPointObject;
}
}
}
else
{
Logger.LogInfo("Spawn Point was empty, selecting random.");
NotificationManagerClass.DisplayMessageNotification("Using random spawn points", iconType: EFT.Communications.ENotificationIconType.Alert);
spawnPoint = SpawnSystem.SelectSpawnPoint(ESpawnCategory.Player, Profile_0.Info.Side);
}
}
}

Expand Down Expand Up @@ -654,7 +672,7 @@ public override async Task<LocalPlayer> vmethod_2(int playerId, Vector3 position
if (spawnPoint == null)
{
Logger.LogWarning("SpawnPoint was null after retrieving it from the server!");
SpawnSystem.SelectSpawnPoint(ESpawnCategory.Player, Profile_0.Info.Side);
spawnPoint = SpawnSystem.SelectSpawnPoint(ESpawnCategory.Player, Profile_0.Info.Side);
}
}

Expand Down Expand Up @@ -683,7 +701,7 @@ public override async Task<LocalPlayer> vmethod_2(int playerId, Vector3 position
CoopPlayer coopPlayer = (CoopPlayer)myPlayer;
coopHandler.Players.Add(coopPlayer.NetId, coopPlayer);

PlayerSpawnRequest body = new PlayerSpawnRequest(myPlayer.ProfileId, MatchmakerAcceptPatches.GetGroupId());
PlayerSpawnRequest body = new(myPlayer.ProfileId, MatchmakerAcceptPatches.GetGroupId());
await FikaRequestHandler.UpdatePlayerSpawn(body);

myPlayer.SpawnPoint = spawnPoint;
Expand All @@ -710,6 +728,8 @@ public override async Task<LocalPlayer> vmethod_2(int playerId, Vector3 position
ErrorScreen.EButtonType.OkButton, 15f, () =>
{
StopFromError(myPlayer.ProfileId, ExitStatus.Runner);
PlayerLeftRequest playerLeftRequest = new(coopPlayer.ProfileId);
FikaRequestHandler.RaidLeave(playerLeftRequest);
}, null);
});
Traverse.Create(backButtonComponent).Field("OnClick").SetValue(newEvent);
Expand Down Expand Up @@ -902,7 +922,7 @@ private async Task WaitForPlayers()

private async Task SetStatus(LocalPlayer myPlayer, LobbyEntry.ELobbyStatus status)
{
SetStatusModel statusBody = new SetStatusModel(myPlayer.ProfileId, status);
SetStatusModel statusBody = new(myPlayer.ProfileId, status);
await FikaRequestHandler.UpdateSetStatus(statusBody);
Logger.LogInfo("Setting game status to: " + status.ToString());
}
Expand Down Expand Up @@ -1482,7 +1502,7 @@ private void StopFromError(string profileId, ExitStatus exitStatus)
string exitName = null;
float delay = 0f;

PlayerLeftRequest body = new PlayerLeftRequest(profileId);
PlayerLeftRequest body = new(profileId);
FikaRequestHandler.RaidLeave(body);

if (CoopHandler.TryGetCoopHandler(out CoopHandler coopHandler))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ public void HandleFirearmPacket(in WeaponPacket packet, InventoryControllerClass
break;
}

if (string.IsNullOrEmpty(packet.ShotInfoPacket.AmmoTemplate))
{
FikaPlugin.Instance.FikaLogger.LogError("CoopObservedFirearmController::HandleFirearmPacket: AmmoTemplate was null or empty!");
return;
}

Weapon.MalfState.MalfunctionedAmmo = (BulletClass)Singleton<ItemFactory>.Instance.CreateItem(MongoID.Generate(), packet.ShotInfoPacket.AmmoTemplate, null);
if (weaponPrefab != null)
{
Expand Down
43 changes: 24 additions & 19 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 @@ -180,6 +180,11 @@ protected override void Start()
{
dynamicAi = gameObject.AddComponent<FikaDynamicAI>();
}

if (FikaPlugin.DisableBotMetabolism.Value)
{
HealthController.DisableMetabolism();
}
}

public override void BtrInteraction()
Expand Down Expand Up @@ -214,22 +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;
yield return new WaitUntil(() => { return MovementContext.IsGrounded; });
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 Expand Up @@ -320,19 +338,6 @@ private IEnumerator DestroyNetworkedComponents()
public override void UpdateTick()
{
base.UpdateTick();

// Temp test to fix AI dying from hydration and energy...
/*if (ActiveHealthController.Energy.Current < 1)
{
logger($"Setting energy to 50 on {ProfileId}");
ActiveHealthController.ChangeEnergy(50);
}
if (ActiveHealthController.Hydration.Current < 1)
{
logger($"Setting hydration to 50 on {ProfileId}");
ActiveHealthController.ChangeHydration(50);
}*/
}

public override void OnDestroy()
Expand Down
8 changes: 6 additions & 2 deletions Fika.Core/Coop/Players/CoopPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ public static async Task<LocalPlayer> Create(
achievementsController.Run();
}

IStatisticsManager statisticsManager = null;
statisticsManager = new CoopClientStatisticsManager(player.Profile);
IStatisticsManager statisticsManager = new CoopClientStatisticsManager(player.Profile);

await player.Init(rotation, layerName, pointOfView, profile, inventoryController,
new CoopClientHealthController(profile.Health, player, inventoryController, profile.Skills, aiControl),
Expand Down Expand Up @@ -219,6 +218,11 @@ public override void ApplyDamageInfo(DamageInfo damageInfo, EBodyPart bodyPartTy
{
damageInfo.Damage *= FikaPlugin.ArmpitDamageMultiplier.Value;
}

if (bodyPartType is EBodyPart.Stomach)
{
damageInfo.Damage *= FikaPlugin.StomachDamageMultiplier.Value;
}
}

base.ApplyDamageInfo(damageInfo, bodyPartType, colliderType, absorbed);
Expand Down
Loading

0 comments on commit ce9f007

Please sign in to comment.