Skip to content

Commit

Permalink
Sync DFCommunicator
Browse files Browse the repository at this point in the history
- metadata cost is set to 0 as in sandbox mode
  • Loading branch information
starfi5h committed Jun 19, 2024
1 parent e68e861 commit 7ef1c5c
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 10 deletions.
15 changes: 15 additions & 0 deletions NebulaModel/Packets/Combat/CombatAggressivenessUpdatePacket.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
15 changes: 15 additions & 0 deletions NebulaModel/Packets/Combat/CombatTruceUpdatePacket.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
Original file line number Diff line number Diff line change
@@ -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<CombatAggressivenessUpdatePacket>
{
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);
}
}
Original file line number Diff line number Diff line change
@@ -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<CombatTruceUpdatePacket>
{
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);
}
}
81 changes: 73 additions & 8 deletions NebulaPatcher/Patches/Dynamic/UIDFCommunicatorWindow_Patch.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#region

using HarmonyLib;
using NebulaModel.Packets.Combat;
using NebulaWorld;
using NebulaWorld.Combat;

#endregion

Expand All @@ -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;
}
}
2 changes: 0 additions & 2 deletions NebulaWorld/Combat/EnemyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ public class EnemyManager : IDisposable

public readonly Dictionary<int, int[]> GroundTargets = [];

public const bool DISABLE_DFCommunicator = true;

private readonly Dictionary<int, DFGUpdateBaseStatusPacket> basePackets = [];
private readonly Dictionary<int, DFHiveUpdateStatusPacket> hivePackets = [];

Expand Down

0 comments on commit 7ef1c5c

Please sign in to comment.