Skip to content
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

Self defense improvements #383

Merged
merged 2 commits into from
Oct 20, 2023
Merged
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
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);
}
Loading