Skip to content

Commit

Permalink
Merge pull request #383 from MUnique/self-defense-improvements
Browse files Browse the repository at this point in the history
Self defense improvements
  • Loading branch information
sven-n authored Oct 20, 2023
2 parents 8b652eb + 7eb852f commit 1a69fbf
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/GameLogic/PlugIns/SelfDefensePlugIn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
// </copyright>

using MUnique.OpenMU.GameLogic.NPC;

namespace MUnique.OpenMU.GameLogic.PlugIns;

using System;
using System.Runtime.InteropServices;
using MUnique.OpenMU.GameLogic.NPC;
using MUnique.OpenMU.GameLogic.Views;
using MUnique.OpenMU.Interfaces;
using MUnique.OpenMU.PlugIns;
Expand All @@ -17,14 +16,16 @@ namespace MUnique.OpenMU.GameLogic.PlugIns;
/// </summary>
[PlugIn(nameof(SelfDefensePlugIn), "Updates the state of the self defense system.")]
[Guid("3E702A15-653A-48EF-899C-4CDB2239A90C")]
public class SelfDefensePlugIn : IPeriodicTaskPlugIn, IAttackableGotHitPlugIn
public class SelfDefensePlugIn : IPeriodicTaskPlugIn, IAttackableGotHitPlugIn, ISupportCustomConfiguration<SelfDefensePlugInConfiguration>, ISupportDefaultCustomConfiguration
{
private readonly TimeSpan _selfDefenseTime = TimeSpan.FromSeconds(60);
/// <inheritdoc />
public SelfDefensePlugInConfiguration? Configuration { get; set; }

/// <inheritdoc />
public async ValueTask ExecuteTaskAsync(GameContext gameContext)
{
var timedOut = gameContext.SelfDefenseState.Where(s => DateTime.UtcNow.Subtract(s.Value) >= _selfDefenseTime).ToList();
var configuration = this.Configuration ??= CreateDefaultConfiguration();
var timedOut = gameContext.SelfDefenseState.Where(s => DateTime.UtcNow.Subtract(s.Value) >= configuration.SelfDefenseTimeOut).ToList();
foreach (var (pair, lastAttack) in timedOut)
{
if (gameContext.SelfDefenseState.Remove(pair, out _))
Expand All @@ -50,6 +51,12 @@ public void AttackableGotHit(IAttackable attackable, IAttacker attacker, HitInfo
return;
}

if (attackerPlayer.CurrentMiniGame?.AllowPlayerKilling is true)
{
// e.g. during chaos castle
return;
}

if (attackerPlayer.IsSelfDefenseActive(defender))
{
// Attacking during self defense period does not initiate another self defense.
Expand All @@ -70,11 +77,22 @@ public void AttackableGotHit(IAttackable attackable, IAttacker attacker, HitInfo
}, (tuple, time) => now);
}

/// <inheritdoc />
public object CreateDefaultConfig()
{
return CreateDefaultConfiguration();
}

private static SelfDefensePlugInConfiguration CreateDefaultConfiguration()
{
return new SelfDefensePlugInConfiguration();
}

private async ValueTask BeginSelfDefenseAsync(Player attacker, Player defender)
{
var message = $"Self defense is initiated by {attacker.Name}'s attack to {defender.Name}!";
await defender.InvokeViewPlugInAsync<IShowMessagePlugIn>(p => p.ShowMessageAsync(message, MessageType.BlueNormal));
await attacker.InvokeViewPlugInAsync<IShowMessagePlugIn>(p => p.ShowMessageAsync(message, MessageType.BlueNormal));
await defender.InvokeViewPlugInAsync<IShowMessagePlugIn>(p => p.ShowMessageAsync(message, MessageType.BlueNormal)).ConfigureAwait(false);
await attacker.InvokeViewPlugInAsync<IShowMessagePlugIn>(p => p.ShowMessageAsync(message, MessageType.BlueNormal)).ConfigureAwait(false);
}

private async ValueTask EndSelfDefenseAsync(Player attacker, Player defender)
Expand Down
16 changes: 16 additions & 0 deletions src/GameLogic/PlugIns/SelfDefensePlugInConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// <copyright file="SelfDefensePlugInConfiguration.cs" company="MUnique">
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
// </copyright>

namespace MUnique.OpenMU.GameLogic.PlugIns;

/// <summary>
/// Configuration for the <see cref="SelfDefensePlugIn"/>.
/// </summary>
public class SelfDefensePlugInConfiguration
{
/// <summary>
/// Gets or sets the self defense timeout.
/// </summary>
public TimeSpan SelfDefenseTimeOut { get; set; } = TimeSpan.FromMinutes(1);
}

0 comments on commit 1a69fbf

Please sign in to comment.