~ruther/NosSmooth

ref: 762fc34c97e8b1f6e9290426b8bc2a78122e28ba NosSmooth/Extensions/NosSmooth.Extensions.Combat/Policies/UseSkillPolicy.cs -rw-r--r-- 1.7 KiB
762fc34c — Rutherther Merge pull request #82 from Rutherther/feat/serialize-stackalloc 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//
//  UseSkillPolicy.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using NosSmooth.Data.Abstractions.Enums;
using NosSmooth.Extensions.Combat.Errors;
using NosSmooth.Extensions.Combat.Selectors;
using NosSmooth.Game.Data.Characters;
using Remora.Results;

namespace NosSmooth.Extensions.Combat.Policies;

/// <summary>
/// The policy to use a skill.
/// </summary>
/// <param name="PreferTargetedSkills">Whether to prefer targeted skills (true) or area skills (false).</param>
/// <param name="AllowedSkillVNums">The vnums of the skills that are allowed to be used.</param>
public record UseSkillPolicy(bool PreferTargetedSkills, int[]? AllowedSkillVNums)
    : ISkillSelector
{
    /// <inheritdoc />
    public Result<Skill> GetSelectedSkill(IEnumerable<Skill> usableSkills)
    {
        var skills = usableSkills.Where(x => CanBeUsed(x))
            .Reverse();

        if (PreferTargetedSkills)
        {
            skills = skills.OrderBy(x => x.Info!.HitType == HitType.EnemiesInZone ? 1 : 0);
        }

        var skill = skills.FirstOrDefault();
        if (skill is null)
        {
            return new SkillNotFoundError();
        }

        return skill;
    }

    private bool CanBeUsed(Skill skill)
    {
        if (AllowedSkillVNums is not null && !AllowedSkillVNums.Contains(skill.SkillVNum))
        {
            return false;
        }

        if (skill.Info is null)
        {
            return false;
        }

        return skill.Info.HitType is HitType.EnemiesInZone or HitType.TargetOnly
            && skill.Info.TargetType is TargetType.Target or TargetType.NoTarget;
    }
}
Do not follow this link