-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathArmorSelector.cs
53 lines (45 loc) · 1.93 KB
/
ArmorSelector.cs
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
using System;
using System.Collections.Generic;
using TaleWorlds.Core;
using TaleWorlds.LinQuick;
using TaleWorlds.MountAndBlade;
namespace Bannerlord.DynamicTroop;
public static class ArmorSelector {
private static readonly Random Random = new();
/// <summary>
/// 根据身体部位从装甲列表中随机选择一件装甲。
/// </summary>
/// <param name="armors"> 装甲列表。 </param>
/// <param name="bodyPart"> 要保护的身体部位。 </param>
/// <returns> 选中的装甲物品,如果没有合适的装甲则返回null。 </returns>
public static ItemObject? GetRandomArmorByBodyPart(List<ItemObject> armors, BoneBodyPartType? bodyPart) {
if (!bodyPart.HasValue) return null;
var weightedArmors = armors.WhereQ(armor => armor.HasArmorComponent)
.SelectQ(armor => new { Armor = armor, armor.ArmorComponent })
.WhereQ(ac => ac.ArmorComponent != null)
.SelectQ(ac => new {
ac.Armor,
Weight =
Helper.GetArmorValueForBodyPart(ac.ArmorComponent,
bodyPart.Value)
})
.WhereQ(aw => aw.Weight > 0)
.ToArrayQ();
return SelectArmorBasedOnWeight(weightedArmors.SelectQ(aw => (aw.Armor, aw.Weight)).ToArrayQ());
}
/// <summary>
/// 基于权重从装甲列表中选择一件装甲。
/// </summary>
/// <param name="weightedArmors"> 包含装甲及其权重的数组。 </param>
/// <returns> 根据权重选中的装甲物品,如果没有选中任何装甲则返回null。 </returns>
private static ItemObject? SelectArmorBasedOnWeight((ItemObject Armor, int Weight)[] weightedArmors) {
var totalWeight = weightedArmors.SumQ(a => a.Weight);
var choice = Random.Next(totalWeight);
var sum = 0;
foreach ((var armor, var weight) in weightedArmors) {
sum += weight;
if (choice < sum) return armor;
}
return null;
}
}