-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add basic sync for PvP #2107
Merged
Merged
Add basic sync for PvP #2107
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
Nitrox.Test/Patcher/Patches/Dynamic/Knife_OnToolUseAnim_PatchTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using FluentAssertions; | ||
using HarmonyLib; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using NitroxTest.Patcher; | ||
|
||
namespace NitroxPatcher.Patches.Dynamic; | ||
|
||
[TestClass] | ||
public class Knife_OnToolUseAnim_PatchTest | ||
{ | ||
[TestMethod] | ||
public void Sanity() | ||
{ | ||
IEnumerable<CodeInstruction> originalIl = PatchTestHelper.GetInstructionsFromMethod(Knife_OnToolUseAnim_Patch.TARGET_METHOD); | ||
IEnumerable<CodeInstruction> transformedIl = Knife_OnToolUseAnim_Patch.Transpiler(originalIl); | ||
transformedIl.Count().Should().Be(originalIl.Count()); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
NitroxClient/Communication/Packets/Processors/PvPAttackProcessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using NitroxClient.Communication.Packets.Processors.Abstract; | ||
using NitroxModel.Packets; | ||
|
||
namespace NitroxClient.Communication.Packets.Processors; | ||
|
||
public class PvPAttackProcessor : ClientPacketProcessor<PvPAttack> | ||
{ | ||
public override void Process(PvPAttack packet) | ||
{ | ||
if (Player.main && Player.main.liveMixin) | ||
{ | ||
Player.main.liveMixin.TakeDamage(packet.Damage); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
|
||
namespace NitroxModel.Packets; | ||
|
||
[Serializable] | ||
public class PvPAttack : Packet | ||
{ | ||
public ushort TargetPlayerId { get; } | ||
public float Damage { get; set; } | ||
public AttackType Type { get; } | ||
|
||
public PvPAttack(ushort targetPlayerId, float damage, AttackType type) | ||
{ | ||
TargetPlayerId = targetPlayerId; | ||
Damage = damage; | ||
Type = type; | ||
} | ||
|
||
public enum AttackType : byte | ||
{ | ||
KnifeHit, | ||
HeatbladeHit | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
NitroxPatcher/Patches/Dynamic/Knife_OnToolUseAnim_Patch.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Reflection.Emit; | ||
using HarmonyLib; | ||
using NitroxModel.Helper; | ||
|
||
namespace NitroxPatcher.Patches.Dynamic; | ||
|
||
/// <summary> | ||
/// Registers knife hits's dealer as the main Player object | ||
/// </summary> | ||
public sealed partial class Knife_OnToolUseAnim_Patch : NitroxPatch, IDynamicPatch | ||
{ | ||
public static readonly MethodInfo TARGET_METHOD = Reflect.Method((Knife t) => t.OnToolUseAnim(default)); | ||
|
||
/* | ||
* | ||
* bool flag = liveMixin.IsAlive(); | ||
* REPLACE below line | ||
* liveMixin.TakeDamage(this.damage, vector, this.damageType, null); | ||
* BY: | ||
dartasen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* liveMixin.TakeDamage(this.damage, vector, this.damageType, Player.mainObject); | ||
* this.GiveResourceOnDamage(gameObject, liveMixin.IsAlive(), flag); | ||
*/ | ||
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions) | ||
{ | ||
return new CodeMatcher(instructions).MatchEndForward([ | ||
new CodeMatch(OpCodes.Ldloc_0), | ||
new CodeMatch(OpCodes.Ldarg_0), | ||
new CodeMatch(OpCodes.Ldfld), | ||
new CodeMatch(OpCodes.Ldnull) | ||
]) | ||
.Set(OpCodes.Ldsfld, Reflect.Field(() => Player.mainObject)) | ||
.InstructionEnumeration(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
NitroxServer/Communication/Packets/Processors/PvPAttackProcessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System.Collections.Generic; | ||
using NitroxModel.Packets; | ||
using NitroxServer.Communication.Packets.Processors.Abstract; | ||
using NitroxServer.GameLogic; | ||
using NitroxServer.Serialization; | ||
|
||
namespace NitroxServer.Communication.Packets.Processors; | ||
|
||
public class PvPAttackProcessor : AuthenticatedPacketProcessor<PvPAttack> | ||
{ | ||
private readonly ServerConfig serverConfig; | ||
private readonly PlayerManager playerManager; | ||
|
||
// TODO: In the future, do a whole config for damage sources | ||
tornac1234 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private static readonly Dictionary<PvPAttack.AttackType, float> damageMultiplierByType = new() | ||
{ | ||
{ PvPAttack.AttackType.KnifeHit, 0.5f }, | ||
Coding-Hen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ PvPAttack.AttackType.HeatbladeHit, 1f } | ||
}; | ||
|
||
public PvPAttackProcessor(ServerConfig serverConfig, PlayerManager playerManager) | ||
{ | ||
this.serverConfig = serverConfig; | ||
this.playerManager = playerManager; | ||
} | ||
|
||
public override void Process(PvPAttack packet, Player player) | ||
{ | ||
if (serverConfig.PvPEnabled && | ||
playerManager.TryGetPlayerById(packet.TargetPlayerId, out Player targetPlayer) && | ||
damageMultiplierByType.TryGetValue(packet.Type, out float multiplier)) | ||
{ | ||
packet.Damage *= multiplier; | ||
targetPlayer.SendPacket(packet); | ||
} | ||
tornac1234 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would log not finding this as an error bc the damage is disappearing without the player sending a health update back to the server.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If local player isn't instantiated, or its live mixin isn't found, then it can only mean that the game is in an early loading state/in an game closing unloading state in which case it's not necessary to log the failure