Skip to content

Commit

Permalink
Add EnemyManager.DisplayPlanetPingMessage and DisplayAstroMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
starfi5h committed Mar 9, 2024
1 parent 705414f commit 44df59a
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ protected override void ProcessPacket(DFTinderDispatchPacket packet, NebulaConne
var hiveSystem = GameMain.spaceSector.GetHiveByAstroId(packet.OriginHiveAstroId);
if (hiveSystem == null) return;

Multiplayer.Session.Enemies.DisplayAstroMessage("Dark Fog seed send out from".Translate(), hiveSystem.starData.astroId);

ref var tinderComponent = ref hiveSystem.tinders.buffer[packet.TinderId];
if (tinderComponent.id != packet.TinderId) return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ public class DFGLaunchAssaultProcessor : PacketProcessor<DFGLaunchAssaultPacket>
protected override void ProcessPacket(DFGLaunchAssaultPacket packet, NebulaConnection conn)
{
var factory = GameMain.galaxy.PlanetById(packet.PlanetId)?.factory;
if (factory == null) return;
if (factory == null)
{
// Display message in chat if it can't show in UIDarkFogMonitor
Multiplayer.Session.Enemies.DisplayPlanetPingMessage("Planetary base is attacking".Translate(), packet.PlanetId, packet.TarPos.ToVector3());
return;
}

using (Multiplayer.Session.Combat.IsIncomingRequest.On())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class DFSLaunchLancerAssaultProcessor : PacketProcessor<DFSLaunchLancerAs
{
protected override void ProcessPacket(DFSLaunchLancerAssaultPacket packet, NebulaConnection conn)
{
Multiplayer.Session.Enemies.DisplayAstroMessage("Space hive is attacking".Translate(), packet.TargetAstroId);
var spaceSector = GameMain.spaceSector;
var hive = spaceSector.GetHiveByAstroId(packet.HiveAstroId);
if (hive == null) return;
Expand Down
2 changes: 1 addition & 1 deletion NebulaPatcher/Patches/Dynamic/DFGBaseComponent_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public static bool LaunchAssault_Prefix(DFGBaseComponent __instance, Vector3 tar
if (Multiplayer.Session.IsClient) return Multiplayer.Session.Combat.IsIncomingRequest.Value;

var packet = new DFGLaunchAssaultPacket(__instance, in tarPos, expandRadius, unitCount0, unitCount1, ap0, ap1, unitThreat);
Multiplayer.Session.Network.SendPacketToStar(packet, __instance.groundSystem.planet.star.id);
Multiplayer.Session.Server.SendPacket(packet);
return true;
}
}
23 changes: 23 additions & 0 deletions NebulaPatcher/Patches/Dynamic/DFRelayComponent_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ public static bool ArriveBase_Prefix(DFRelayComponent __instance)
return true;
}

[HarmonyPostfix]
[HarmonyPatch(nameof(DFRelayComponent.ArriveBase))]
public static void ArriveBase_Postfix(DFRelayComponent __instance)
{
if (!Multiplayer.IsActive) return;

var planet = GameMain.galaxy.PlanetById(__instance.targetAstroId);
if (planet != null && __instance.baseId > 0)
{
Multiplayer.Session.Enemies.DisplayPlanetPingMessage("Relay lands on planet".Translate(), __instance.targetAstroId, __instance.targetLPos);
}
}

[HarmonyPrefix]
[HarmonyPatch(nameof(DFRelayComponent.ArriveDock))]
public static bool ArriveDock_Prefix(DFRelayComponent __instance)
Expand All @@ -49,6 +62,16 @@ public static bool ArriveDock_Prefix(DFRelayComponent __instance)
public static bool LeaveBase_Prefix(DFRelayComponent __instance)
{
if (!Multiplayer.IsActive) return true;

if (Multiplayer.Session.IsServer || Multiplayer.Session.Enemies.IsIncomingRelayRequest)
{
var planet = GameMain.galaxy.PlanetById(__instance.targetAstroId);
if (planet != null)
{
Multiplayer.Session.Enemies.DisplayPlanetPingMessage("Relay leaves from planet", __instance.targetAstroId, __instance.targetLPos);
}
}

if (Multiplayer.Session.IsClient) return Multiplayer.Session.Enemies.IsIncomingRelayRequest;

Multiplayer.Session.Network.SendPacket(new DFRelayLeaveBasePacket(__instance));
Expand Down
1 change: 1 addition & 0 deletions NebulaPatcher/Patches/Dynamic/DFTinderComponent_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public static bool DispatchFromHive_Prefix(ref DFTinderComponent __instance, int
{
__instance.targetHiveAstroId = _targetHiveAstroId;
Multiplayer.Session.Network.SendPacket(new DFTinderDispatchPacket(__instance));
Multiplayer.Session.Enemies.DisplayAstroMessage("Dark Fog seed send out from".Translate(), hive1.starData.astroId);
}
return true;
}
Expand Down
2 changes: 2 additions & 0 deletions NebulaPatcher/Patches/Dynamic/EnemyDFHiveSystem_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using HarmonyLib;
using NebulaAPI.DataStructures;
using NebulaModel.Packets.Combat.DFHive;
using NebulaModel.Packets.Combat.SpaceEnemy;
using NebulaWorld;
Expand Down Expand Up @@ -236,6 +237,7 @@ public static bool LaunchLancerAssault_Prefix(EnemyDFHiveSystem __instance, EAgg
var packet = new DFSLaunchLancerAssaultPacket(in __instance, aggressiveLevel,
in tarPos, in maxHatredPos, targetAstroId, unitCount0, unitThreat);
Multiplayer.Session.Server.SendPacket(packet);
Multiplayer.Session.Enemies.DisplayAstroMessage("Space hive is attacking".Translate(), packet.TargetAstroId);
return true;
}

Expand Down
4 changes: 1 addition & 3 deletions NebulaPatcher/Patches/Dynamic/GameData_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,6 @@ public static void GameTick_Postfix(GameData __instance, long time)
Multiplayer.Session.Couriers.GameTick();
Multiplayer.Session.Belts.GameTick();
Multiplayer.Session.Combat.GameTick();
Multiplayer.Session.Enemies.GameTick(time);

if (Multiplayer.Session.LocalPlayer.IsHost)
{
Expand Down Expand Up @@ -426,8 +425,7 @@ public static void LeavePlanet_Prefix()
{
Multiplayer.Session.Trashes.Refresh();
Multiplayer.Session.PowerTowers.ResetAndBroadcast();
//todo:replace
//GameMain.mainPlayer.mecha.droneLogic.serving.Clear();
Multiplayer.Session.Enemies.OnLeavePlanet();
}
}

Expand Down
72 changes: 56 additions & 16 deletions NebulaWorld/Combat/EnemyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
using System;
using System.Collections.Generic;
using NebulaModel.DataStructures;
using NebulaModel.DataStructures.Chat;
using NebulaModel.Packets.Combat.DFHive;
using NebulaModel.Packets.Combat.GroundEnemy;
using NebulaWorld.Chat.ChatLinks;
using NebulaWorld.MonoBehaviours.Local.Chat;
using UnityEngine;
#pragma warning disable IDE1006 // Naming Styles
#pragma warning disable CA1822 // Mark members as static

#endregion

Expand All @@ -17,19 +22,13 @@ public class EnemyManager : IDisposable

public readonly ToggleSwitch IsIncomingRelayRequest = new();

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

public const bool DISABLE_DFCommunicator = true;

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

public EnemyManager()
{
GroundTargets.Clear();
basePackets.Clear();
hivePackets.Clear();
}

public void Dispose()
{
Expand All @@ -39,13 +38,6 @@ public void Dispose()
GC.SuppressFinalize(this);
}

public void GameTick(long gameTick)
{
if (!Multiplayer.Session.IsGameLoaded) return;
// Place holder for future
var _ = gameTick;
}

public void BroadcastBaseStatusPackets(EnemyDFGroundSystem enemySystem, long gameTick)
{
var factoryIndex = enemySystem.factory.index;
Expand All @@ -62,9 +54,9 @@ public void BroadcastBaseStatusPackets(EnemyDFGroundSystem enemySystem, long gam
basePackets.Add(hashId, packet);
}
var levelChanged = packet.Level != dFbase.evolve.level;
if (levelChanged || (hashId % 300) == (int)gameTick % 300)
if (levelChanged || (hashId % 120) == (int)gameTick % 120)
{
// Update when base level changes, or every 5s
// Update when base level changes, or every 2s to players on that planet
packet.Record(in dFbase);
var planetId = enemySystem.planet.id;
if (levelChanged)
Expand Down Expand Up @@ -95,6 +87,33 @@ public void BroadcastHiveStatusPackets(EnemyDFHiveSystem hive, long gameTick)
}
}

public void DisplayPlanetPingMessage(string text, int planetId, Vector3 pos)
{
var planet = GameMain.galaxy.PlanetById(planetId);
if (planet == null) return;

var message = text + " [" + NavigateChatLinkHandler.FormatNavigateToPlanetPos(planetId, pos, planet.displayName) + "]";
ChatManager.Instance.SendChatMessage(message, ChatMessageType.BattleMessage);
}

public void DisplayAstroMessage(string text, int astroId)
{
string displayMessage = null;

if (GameMain.galaxy.PlanetById(astroId) != null)
{
displayMessage = GameMain.galaxy.PlanetById(astroId).displayName;
}
else if (GameMain.galaxy.StarById(astroId / 100) != null)
{
displayMessage = GameMain.galaxy.StarById(astroId / 100).displayName;
}
if (displayMessage == null) return;

var message = text + " [" + NavigateChatLinkHandler.FormatNavigateToAstro(astroId, displayMessage) + "]";
ChatManager.Instance.SendChatMessage(message, ChatMessageType.BattleMessage);
}

public void OnFactoryLoadFinished(PlanetFactory factory)
{
var planetId = factory.planetId;
Expand All @@ -111,6 +130,27 @@ public void OnFactoryLoadFinished(PlanetFactory factory)
}
}

public void OnLeavePlanet()
{
if (Multiplayer.Session.IsServer) return;

// Reset threat on each base on the loaded factory so it doesn't show on the monitor
for (var factoryIdx = 0; factoryIdx < GameMain.data.factoryCount; factoryIdx++)
{
var factory = GameMain.data.factories[factoryIdx];
if (factory == null) continue;

var bases = factory.enemySystem.bases;
for (var baseId = 1; baseId < bases.cursor; baseId++)
{
if (bases[baseId] != null && bases[baseId].id == baseId && !bases[baseId].hasAssaultingUnit)
{
bases[baseId].evolve.threat = 0;
}
}
}
}

public static void SetPlanetFactoryNextEnemyId(PlanetFactory factory, int enemyId)
{
if (enemyId >= factory.enemyCursor)
Expand Down

0 comments on commit 44df59a

Please sign in to comment.