Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#region
#region

using NebulaAPI.Packets;
using NebulaModel.Networking;
Expand All @@ -19,7 +19,7 @@ protected override void ProcessPacket(DFGRetargetPacket packet, NebulaConnection
if (factory == null) return;

var targets = Multiplayer.Session.Enemies.GroundTargets[packet.PlanetId];
if (packet.EnemyId <= targets.Length)
if (packet.EnemyId >= 0 && packet.EnemyId < targets.Length)
{
targets[packet.EnemyId] = packet.Target;
}
Expand Down
4 changes: 2 additions & 2 deletions NebulaPatcher/Patches/Dynamic/DFGBaseComponent_Patch.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#region
#region

using System;
using HarmonyLib;
Expand Down Expand Up @@ -27,7 +27,7 @@ public static bool UnderAttack_Prefix1()
public static bool ActiveAllUnit_Prefix(DFGBaseComponent __instance, long gameTick)
{
if (!Multiplayer.IsActive) return true;
if (!Multiplayer.Session.Combat.IsIncomingRequest.Value) return false;
if (Multiplayer.Session.IsClient && !Multiplayer.Session.Combat.IsIncomingRequest.Value) return false;

// This will only trigger in event instead of every actived tick
ref var ptr = ref __instance.groundSystem.units.buffer;
Expand Down
36 changes: 34 additions & 2 deletions NebulaPatcher/Patches/Dynamic/EnemyDFGroundSystem_Patch.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#region
#region

using System;
using HarmonyLib;
Expand Down Expand Up @@ -69,13 +69,45 @@ public static bool ExecuteDeferredUnitFormation_Prefix(EnemyDFGroundSystem __ins
{
// Wait for server to authorize
__instance._initiate_unit_list?.Clear();
__instance._activate_unit_list?.Clear();
__instance._deactivate_unit_list?.Clear();
return false;
}

// Skip InitiateUnit in multiplayer game
__instance._initiate_unit_list?.Clear();
return true;
if (__instance._activate_unit_list?.Count > 0)
{
var planetId = __instance.planet.id;
var starId = __instance.planet.star.id;
foreach (var item in __instance._activate_unit_list)
{
int unitId = __instance.ActivateUnit(item.baseId, item.formId, item.port, item.gameTick);
if (unitId > 0)
{
ref var ptr = ref __instance.units.buffer[unitId];
if (ptr.id == unitId)
{
ptr.stateTick = item.stateTick;
ptr.behavior = item.behavior;

var packet = new DFGActivateUnitPacket(planetId, item.baseId, item.formId, item.port,
item.behavior, item.stateTick, ptr.enemyId);
Multiplayer.Session.Network.SendPacketToStar(packet, starId);
}
}
}
__instance._activate_unit_list.Clear();
}
if (__instance._deactivate_unit_list?.Count > 0)
{
foreach (var unitId in __instance._deactivate_unit_list)
{
__instance.DeactivateUnit(unitId);
}
__instance._deactivate_unit_list.Clear();
}
return false;
}

[HarmonyPrefix]
Expand Down
23 changes: 5 additions & 18 deletions NebulaPatcher/Patches/Dynamic/SpaceSector_Patch.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#region
#region

using HarmonyLib;
using NebulaWorld;
Expand All @@ -19,9 +19,8 @@ public static void SetForNewGame_Prefix(SpaceSector __instance)
{
if (Multiplayer.IsActive && Multiplayer.Session.IsClient)
{
// Set this.isCombatMode to false to skip the part of initial enemyDFHiveSystem
// Will revert it to the correct value during SpaceSector.Import called in GameStatesManager.OverwriteGlobalGameData
__instance.isCombatMode = false;
// Ensure isCombatMode is properly set so SectorModel and PrefabDescByModelIndex are initialized on client
__instance.isCombatMode = __instance.gameData?.gameDesc?.isCombatMode ?? true;
}
}

Expand Down Expand Up @@ -89,19 +88,7 @@ public static void GameTick_Prefix(SpaceSector __instance)
{
if (!Multiplayer.IsActive || Multiplayer.Session.IsServer) return;

// Fix NRE in DFSTurretComponent.InternalUpdate (PrefabDesc pdesc);(IL_0017)
for (var enemyId = 1; enemyId < __instance.enemyCursor; enemyId++)
{
ref var enemy = ref __instance.enemyPool[enemyId];
if (enemy.id != enemyId) continue;

if (SpaceSector.PrefabDescByModelIndex[enemy.modelIndex] == null)
{
var msg = $"Remove SpaceSector enemy[{enemyId}]: modelIndex{enemy.modelIndex}";
Log.WarnInform(msg);

__instance.enemyPool[enemyId].SetEmpty();
}
}
// Prevent purging valid space enemies with SetEmpty()
if (__instance.model == null || SpaceSector.PrefabDescByModelIndex == null) return;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#region
#region

using System.Collections.Generic;
using System.Reflection;
Expand Down Expand Up @@ -72,6 +72,7 @@ public static IEnumerable<CodeInstruction> GameTickLogic_Prepare_Transpiler(IEnu

[HarmonyTranspiler]
[HarmonyPatch(nameof(EnemyDFGroundSystem.GameTickLogic_Unit))]
[HarmonyPatch(typeof(GameLogic), nameof(GameLogic._enemy_ground_unit_parallel))]
public static IEnumerable<CodeInstruction> GameTickLogic_Unit_Transpiler(IEnumerable<CodeInstruction> instructions)
{
try
Expand Down
9 changes: 3 additions & 6 deletions NebulaWorld/Combat/CombatManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#region
#region

using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -263,10 +263,7 @@ public void OnFactoryLoadFinished(PlanetFactory factory)
{
factory.vegePool[i].combatStatId = 0;
}
for (var i = 1; i < factory.enemyCursor; i++)
{
factory.enemyPool[i].combatStatId = 0;
}
// Do not erase enemy combatStatId to preserve HP and shields on client
for (var i = 1; i < factory.veinCursor; i++)
{
factory.veinPool[i].combatStatId = 0;
Expand All @@ -281,7 +278,7 @@ public void OnFactoryLoadFinished(PlanetFactory factory)
for (var i = 1; i < combatStatCursor; i++)
{
ref var ptr = ref combatStatbuffer[i];
if (ptr.id == i && ptr.astroId == astroId)
if (ptr.id == i && ptr.astroId == astroId && ptr.objectType != (int)EObjectType.Enemy)
{
combatStats.Remove(i);
count++;
Expand Down
55 changes: 50 additions & 5 deletions NebulaWorld/Combat/EnemyManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#region
#region

using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -187,8 +187,30 @@ public static void SetPlanetFactoryNextEnemyId(PlanetFactory factory, int enemyI
factory.KillEnemyFinally(enemyId, ref CombatStat.empty);
}

factory.enemyRecycle[0] = enemyId;
factory.enemyRecycleCursor = 1;
var found = false;
for (var i = 0; i < factory.enemyRecycleCursor; i++)
{
if (factory.enemyRecycle[i] == enemyId)
{
found = true;
var lastIdx = factory.enemyRecycleCursor - 1;
if (i != lastIdx)
{
factory.enemyRecycle[i] = factory.enemyRecycle[lastIdx];
factory.enemyRecycle[lastIdx] = enemyId;
}
break;
}
}

if (!found)
{
if (factory.enemyRecycleCursor >= factory.enemyRecycle.Length)
{
Array.Resize(ref factory.enemyRecycle, Math.Max(factory.enemyRecycle.Length * 2, factory.enemyRecycleCursor + 1));
}
factory.enemyRecycle[factory.enemyRecycleCursor++] = enemyId;
}
}
}

Expand Down Expand Up @@ -246,8 +268,31 @@ public static void SetSpaceSectorNextEnemyId(int enemyId)
ptr.isInvincible = false;
spaceSector.KillEnemyFinal(enemyId, ref CombatStat.empty);
}
spaceSector.enemyRecycle[0] = enemyId;
spaceSector.enemyRecycleCursor = 1;

var found = false;
for (var i = 0; i < spaceSector.enemyRecycleCursor; i++)
{
if (spaceSector.enemyRecycle[i] == enemyId)
{
found = true;
var lastIdx = spaceSector.enemyRecycleCursor - 1;
if (i != lastIdx)
{
spaceSector.enemyRecycle[i] = spaceSector.enemyRecycle[lastIdx];
spaceSector.enemyRecycle[lastIdx] = enemyId;
}
break;
}
}

if (!found)
{
if (spaceSector.enemyRecycleCursor >= spaceSector.enemyRecycle.Length)
{
Array.Resize(ref spaceSector.enemyRecycle, Math.Max(spaceSector.enemyRecycle.Length * 2, spaceSector.enemyRecycleCursor + 1));
}
spaceSector.enemyRecycle[spaceSector.enemyRecycleCursor++] = enemyId;
}
}
}

Expand Down