From 7ef1c5c6d943eb15734ea86204643e750985ba1b Mon Sep 17 00:00:00 2001 From: starfish <50672801+starfi5h@users.noreply.github.com> Date: Wed, 19 Jun 2024 10:39:06 +0800 Subject: [PATCH] Sync DFCommunicator - metadata cost is set to 0 as in sandbox mode --- .../CombatAggressivenessUpdatePacket.cs | 15 ++++ .../Packets/Combat/CombatTruceUpdatePacket.cs | 15 ++++ .../Combat/CombatAggressivenessProcessor.cs | 52 ++++++++++++ .../Combat/CombatTruceUpdateProcessor.cs | 43 ++++++++++ .../Dynamic/UIDFCommunicatorWindow_Patch.cs | 81 +++++++++++++++++-- NebulaWorld/Combat/EnemyManager.cs | 2 - 6 files changed, 198 insertions(+), 10 deletions(-) create mode 100644 NebulaModel/Packets/Combat/CombatAggressivenessUpdatePacket.cs create mode 100644 NebulaModel/Packets/Combat/CombatTruceUpdatePacket.cs create mode 100644 NebulaNetwork/PacketProcessors/Combat/CombatAggressivenessProcessor.cs create mode 100644 NebulaNetwork/PacketProcessors/Combat/CombatTruceUpdateProcessor.cs diff --git a/NebulaModel/Packets/Combat/CombatAggressivenessUpdatePacket.cs b/NebulaModel/Packets/Combat/CombatAggressivenessUpdatePacket.cs new file mode 100644 index 000000000..9d4afb58b --- /dev/null +++ b/NebulaModel/Packets/Combat/CombatAggressivenessUpdatePacket.cs @@ -0,0 +1,15 @@ +namespace NebulaModel.Packets.Combat; + +public class CombatAggressivenessUpdatePacket +{ + public CombatAggressivenessUpdatePacket() { } + + public CombatAggressivenessUpdatePacket(ushort playerId, float aggressiveness) + { + PlayerId = playerId; + Aggressiveness = aggressiveness; + } + + public ushort PlayerId { get; set; } + public float Aggressiveness { get; set; } +} diff --git a/NebulaModel/Packets/Combat/CombatTruceUpdatePacket.cs b/NebulaModel/Packets/Combat/CombatTruceUpdatePacket.cs new file mode 100644 index 000000000..974ce5ce1 --- /dev/null +++ b/NebulaModel/Packets/Combat/CombatTruceUpdatePacket.cs @@ -0,0 +1,15 @@ +namespace NebulaModel.Packets.Combat; + +public class CombatTruceUpdatePacket +{ + public CombatTruceUpdatePacket() { } + + public CombatTruceUpdatePacket(ushort playerId, long truceEndTime) + { + PlayerId = playerId; + TruceEndTime = truceEndTime; + } + + public ushort PlayerId { get; set; } + public long TruceEndTime { get; set; } +} diff --git a/NebulaNetwork/PacketProcessors/Combat/CombatAggressivenessProcessor.cs b/NebulaNetwork/PacketProcessors/Combat/CombatAggressivenessProcessor.cs new file mode 100644 index 000000000..2275e65f7 --- /dev/null +++ b/NebulaNetwork/PacketProcessors/Combat/CombatAggressivenessProcessor.cs @@ -0,0 +1,52 @@ +#region + +using NebulaAPI.Packets; +using NebulaModel.DataStructures.Chat; +using NebulaModel.Networking; +using NebulaModel.Packets; +using NebulaModel.Packets.Combat; +using NebulaWorld; +using NebulaWorld.MonoBehaviours.Local.Chat; + +#endregion + +namespace NebulaNetwork.PacketProcessors.Combat; + +[RegisterPacketProcessor] +public class CombatAggressivenessProcessor : PacketProcessor +{ + protected override void ProcessPacket(CombatAggressivenessUpdatePacket packet, NebulaConnection conn) + { + if (IsHost) + { + Multiplayer.Session.Network.SendPacketExclude(packet, conn); + } + + var history = GameMain.history; + var oldAggressiveLevel = history.combatSettings.aggressiveLevel; + history.combatSettings.aggressiveness = packet.Aggressiveness; + + var currentPropertyMultiplier = history.currentPropertyMultiplier; + if (currentPropertyMultiplier < history.minimalPropertyMultiplier) + { + history.minimalPropertyMultiplier = currentPropertyMultiplier; + } + var difficulty = history.combatSettings.difficulty; + if (difficulty < history.minimalDifficulty) + { + history.minimalDifficulty = difficulty; + } + GameMain.data.spaceSector.OnDFAggressivenessChanged(oldAggressiveLevel, history.combatSettings.aggressiveLevel); + + var userName = ""; + using (Multiplayer.Session.World.GetRemotePlayersModels(out var remotePlayersModels)) + { + if (remotePlayersModels.TryGetValue(packet.PlayerId, out var player)) + { + userName = player.Username; + } + } + var message = string.Format("{0} set DF Aggressiveness {1} => {2}".Translate(), userName, oldAggressiveLevel, history.combatSettings.aggressiveLevel); + ChatManager.Instance.SendChatMessage(message, ChatMessageType.BattleMessage); + } +} diff --git a/NebulaNetwork/PacketProcessors/Combat/CombatTruceUpdateProcessor.cs b/NebulaNetwork/PacketProcessors/Combat/CombatTruceUpdateProcessor.cs new file mode 100644 index 000000000..394617e0c --- /dev/null +++ b/NebulaNetwork/PacketProcessors/Combat/CombatTruceUpdateProcessor.cs @@ -0,0 +1,43 @@ +#region + +using NebulaAPI.Packets; +using NebulaModel.DataStructures.Chat; +using NebulaModel.Networking; +using NebulaModel.Packets; +using NebulaModel.Packets.Combat; +using NebulaWorld; +using NebulaWorld.MonoBehaviours.Local.Chat; + +#endregion + +namespace NebulaNetwork.PacketProcessors.Combat; + +[RegisterPacketProcessor] +public class CombatTruceUpdateProcessor : PacketProcessor +{ + protected override void ProcessPacket(CombatTruceUpdatePacket packet, NebulaConnection conn) + { + if (IsHost) + { + Multiplayer.Session.Network.SendPacketExclude(packet, conn); + } + + var truceTime = packet.TruceEndTime - (GameMain.gameTick + GameMain.history.dfTruceTimer); + GameMain.history.AddTruceTime(truceTime); + + var userName = ""; + using (Multiplayer.Session.World.GetRemotePlayersModels(out var remotePlayersModels)) + { + if (remotePlayersModels.TryGetValue(packet.PlayerId, out var player)) + { + userName = player.Username; + } + } + var message = userName + " set "; + var second = (int)(GameMain.history.dfTruceTimer / 60L); + var minute = second / 60; + var hour = minute / 60; + message += string.Format("停战时间".Translate(), hour, minute % 60, second % 60); + ChatManager.Instance.SendChatMessage(message, ChatMessageType.BattleMessage); + } +} diff --git a/NebulaPatcher/Patches/Dynamic/UIDFCommunicatorWindow_Patch.cs b/NebulaPatcher/Patches/Dynamic/UIDFCommunicatorWindow_Patch.cs index f8e80d62d..b6f701eb3 100644 --- a/NebulaPatcher/Patches/Dynamic/UIDFCommunicatorWindow_Patch.cs +++ b/NebulaPatcher/Patches/Dynamic/UIDFCommunicatorWindow_Patch.cs @@ -1,8 +1,8 @@ #region using HarmonyLib; +using NebulaModel.Packets.Combat; using NebulaWorld; -using NebulaWorld.Combat; #endregion @@ -13,16 +13,81 @@ internal class UIDFCommunicatorWindow_Patch { [HarmonyPrefix] [HarmonyPatch(nameof(UIDFCommunicatorWindow._OnOpen))] - public static bool UIDFCommunicatorWindow_OnOpen_Prefix(UIDFCommunicatorWindow __instance) + public static void OnOpen_Prefix(UIDFCommunicatorWindow __instance, ref bool __state) { - if (!Multiplayer.IsActive) return true; + if (!Multiplayer.IsActive) return; - if (EnemyManager.DISABLE_DFCommunicator) + //Set this.isSandbox = true to remove metadata cost in multiplayer + __state = __instance.gameData.gameDesc.isSandboxMode; + __instance.gameData.gameDesc.isSandboxMode = true; + } + + [HarmonyPostfix] + [HarmonyPatch(nameof(UIDFCommunicatorWindow._OnOpen))] + public static void OnOpen_Postfix(UIDFCommunicatorWindow __instance, bool __state) + { + if (!Multiplayer.IsActive) return; + + __instance.gameData.gameDesc.isSandboxMode = __state; + } + + [HarmonyPrefix] + [HarmonyPatch(nameof(UIDFCommunicatorWindow.OnTruceButtonClicked))] + public static void OnTruceButtonClicked_Prefix(ref long __state) + { + if (!Multiplayer.IsActive) return; + + __state = GameMain.history.dfTruceTimer; + } + + [HarmonyPostfix] + [HarmonyPatch(nameof(UIDFCommunicatorWindow.OnTruceButtonClicked))] + public static void OnTruceButtonClicked_Postfix(long __state) + { + if (!Multiplayer.IsActive) return; + + if (__state != GameMain.history.dfTruceTimer) + { + //If truce is signed, broadcast to other players + var truceEndTime = GameMain.gameTick + GameMain.history.dfTruceTimer; + Multiplayer.Session.Network.SendPacket(new CombatTruceUpdatePacket( + Multiplayer.Session.LocalPlayer.Id, truceEndTime)); + } + } + + [HarmonyPostfix] + [HarmonyPatch(nameof(UIDFCommunicatorWindow.WithdrawTruceConfirm))] + public static void WithdrawTruceConfirm_Postfix() + { + if (!Multiplayer.IsActive) return; + + Multiplayer.Session.Network.SendPacket(new CombatTruceUpdatePacket( + Multiplayer.Session.LocalPlayer.Id, GameMain.gameTick)); + } + + [HarmonyPrefix] + [HarmonyPatch(nameof(UIDFCommunicatorWindow.OnAggressiveIncButtonClicked))] + [HarmonyPatch(nameof(UIDFCommunicatorWindow.OnAggressiveDecButtonClicked))] + public static void OnAggressiveButtonClicked_Prefix(ref float __state) + { + if (!Multiplayer.IsActive) return; + + __state = GameMain.data.history.combatSettings.aggressiveness; + } + + [HarmonyPostfix] + [HarmonyPatch(nameof(UIDFCommunicatorWindow.OnAggressiveIncButtonClicked))] + [HarmonyPatch(nameof(UIDFCommunicatorWindow.OnAggressiveDecButtonClicked))] + public static void OnAggressiveButtonClicked_Postfix(float __state) + { + if (!Multiplayer.IsActive) return; + + var history = GameMain.history; + if (__state != history.combatSettings.aggressiveness) { - InGamePopup.ShowInfo("Unavailable".Translate(), "Dark Fog Communicator is disabled in multiplayer game.".Translate(), - "OK".Translate()); - __instance._Close(); + //If aggressiveness has changed, broadcast to other players + Multiplayer.Session.Network.SendPacket(new CombatAggressivenessUpdatePacket( + Multiplayer.Session.LocalPlayer.Id, history.combatSettings.aggressiveness)); } - return false; } } diff --git a/NebulaWorld/Combat/EnemyManager.cs b/NebulaWorld/Combat/EnemyManager.cs index 34cd17118..d79d06a5d 100644 --- a/NebulaWorld/Combat/EnemyManager.cs +++ b/NebulaWorld/Combat/EnemyManager.cs @@ -25,8 +25,6 @@ public class EnemyManager : IDisposable public readonly Dictionary GroundTargets = []; - public const bool DISABLE_DFCommunicator = true; - private readonly Dictionary basePackets = []; private readonly Dictionary hivePackets = [];