Skip to content

Commit

Permalink
Sync damage to space enemy by mecha fleet
Browse files Browse the repository at this point in the history
  • Loading branch information
starfi5h committed Jul 19, 2024
1 parent e90591a commit bbe209b
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
29 changes: 29 additions & 0 deletions NebulaModel/Packets/Combat/CombatStatDamagePacket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Security.Policy;

namespace NebulaModel.Packets.Combat;

public class CombatStatDamagePacket
{
public CombatStatDamagePacket() { }

public CombatStatDamagePacket(int damage, int slice, in SkillTarget target, in SkillTarget caster)
{
Damage = damage;
Slice = slice;
TargetType = (short)target.type;
TargetId = target.id;
TargetAstroId = target.astroId;
CasterType = (short)caster.type;
CasterId = caster.id;
CasterAstroId = caster.astroId;
}

public int Damage { get; set; }
public int Slice { get; set; }
public short TargetType { get; set; }
public int TargetId { get; set; }
public int TargetAstroId { get; set; }
public short CasterType { get; set; }
public int CasterId { get; set; }
public int CasterAstroId { get; set; }
}
54 changes: 54 additions & 0 deletions NebulaNetwork/PacketProcessors/Combat/CombatStatDamageProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#region

using NebulaAPI.Packets;
using NebulaModel.Networking;
using NebulaModel.Packets;
using NebulaModel.Packets.Combat;
using NebulaWorld;

#endregion

namespace NebulaNetwork.PacketProcessors.Combat;

[RegisterPacketProcessor]
public class CombatStatDamageProcessor : PacketProcessor<CombatStatDamagePacket>
{
protected override void ProcessPacket(CombatStatDamagePacket packet, NebulaConnection conn)
{
if (IsHost)
{
Multiplayer.Session.Server.SendPacketExclude(packet, conn);
}

SkillTarget target;
target.type = (ETargetType)packet.TargetType;
target.id = packet.TargetId;
target.astroId = packet.TargetAstroId;
SkillTarget caster;
caster.type = (ETargetType)packet.CasterType;
caster.id = packet.CasterId;
caster.astroId = packet.CasterAstroId;

if (target.type != ETargetType.Enemy) return; // Guard

var astroId = target.astroId;
if (astroId > 1000000)
{
if (target.id >= GameMain.spaceSector.enemyPool.Length)
{
// Return if enemyId is not exist in client
return;
}
}
else if (astroId > 100 && astroId <= 204899 && astroId % 100 > 0)
{
var factory = GameMain.spaceSector.skillSystem.astroFactories[astroId];
if (factory == null || target.id >= factory.enemyPool.Length) return;
}

using (Multiplayer.Session.Combat.IsIncomingRequest.On())
{
GameMain.spaceSector.skillSystem.DamageObject(packet.Damage, packet.Slice, ref target, ref caster);
}
}
}
16 changes: 16 additions & 0 deletions NebulaPatcher/Patches/Dynamic/SkillSystem_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

#endregion
Expand Down Expand Up @@ -85,4 +86,19 @@ public static bool MechaEnergyShieldResist_Prefix(SkillSystem __instance, ref bo
__result = true;
return false;
}

[HarmonyPrefix]
[HarmonyPatch(nameof(SkillSystem.DamageObject))]
public static void DamageObject_Prefix(int damage, int slice, ref SkillTarget target, ref SkillTarget caster)
{
if (caster.type != ETargetType.Craft || target.type != ETargetType.Enemy
|| target.astroId <= 1000000 // Only sync for space target
|| !Multiplayer.IsActive || Multiplayer.Session.Combat.IsIncomingRequest.Value) return;

var packet = new CombatStatDamagePacket(damage, slice, in target, in caster);
// Change the caster to player as craft (space fleet) is not sync yet
packet.CasterType = (short)ETargetType.Player;
packet.CasterId = Multiplayer.Session.LocalPlayer.Id;
Multiplayer.Session.Network.SendPacket(packet);
}
}

0 comments on commit bbe209b

Please sign in to comment.