~ruther/NosSmooth

ref: 762fc34c97e8b1f6e9290426b8bc2a78122e28ba NosSmooth/Extensions/NosSmooth.Extensions.Combat/Policies/UseItemPolicy.cs -rw-r--r-- 2.8 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
//  UseItemPolicy.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.Extensions.Combat.Errors;
using NosSmooth.Extensions.Combat.Selectors;
using NosSmooth.Game.Data.Characters;
using NosSmooth.Game.Data.Items;
using Remora.Results;

namespace NosSmooth.Extensions.Combat.Policies;

/// <summary>
/// The policy to use an item.
/// </summary>
/// <param name="UseItems">Whether to use items.</param>
/// <param name="UseBelowHealthPercentage">Use items below the given character's health percentage.</param>
/// <param name="UseBelowManaPercentage">Use items below the given character's mana percentage.</param>
/// <param name="UseHealthItemsVNums">The vnums of the items to use as health items.</param>
/// <param name="UseManaItemsVNums">The vnums of the items to use as mana items.</param>
public record UseItemPolicy
(
    bool UseItems,
    int UseBelowHealthPercentage,
    int UseBelowManaPercentage,
    int[] UseHealthItemsVNums,
    int[] UseManaItemsVNums
) : IItemSelector
{
    /// <inheritdoc />
    public Result<InventoryItem> GetSelectedItem(ICombatState combatState, ICollection<InventoryItem> possibleItems)
    {
        var character = combatState.Game.Character;
        if (character is null)
        {
            return new ItemNotFoundError();
        }

        if (ShouldUseHpItem(character))
        {
            var item = possibleItems.Cast<InventoryItem?>().FirstOrDefault
                (x => UseHealthItemsVNums.Contains(x?.Item.Item?.ItemVNum ?? -1));
            if (item is not null)
            {
                return item.Value;
            }
        }

        if (ShouldUseMpItem(character))
        {
            var item = possibleItems.Cast<InventoryItem?>().FirstOrDefault
                (x => UseManaItemsVNums.Contains(x?.Item.Item?.ItemVNum ?? -1));
            if (item is not null)
            {
                return item.Value;
            }
        }

        return new ItemNotFoundError();
    }

    /// <inheritdoc />
    public Result<bool> ShouldUseItem(ICombatState combatState)
    {
        if (!UseItems)
        {
            return false;
        }

        var character = combatState.Game.Character;
        if (character is null)
        {
            return false;
        }

        return ShouldUseHpItem(character) || ShouldUseMpItem(character);
    }

    private bool ShouldUseHpItem(Character character)
    {
        return character.Hp is not null && character.Hp.Percentage is not null
            && character.Hp.Percentage < UseBelowHealthPercentage;
    }

    private bool ShouldUseMpItem(Character character)
    {
        return character.Mp is not null && character.Mp.Percentage is not null
            && character.Mp.Percentage < UseBelowManaPercentage;
    }
}
Do not follow this link