-
Notifications
You must be signed in to change notification settings - Fork 3
/
Assignment.cs
205 lines (162 loc) · 7.1 KB
/
Assignment.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using System;
using System.Collections.Generic;
using System.Threading;
using Bannerlord.DynamicTroop.Extensions;
using TaleWorlds.CampaignSystem;
using TaleWorlds.Core;
using TaleWorlds.LinQuick;
using static TaleWorlds.Core.ItemObject;
namespace Bannerlord.DynamicTroop;
public class Assignment : IComparable {
public static readonly EquipmentIndex[] WeaponSlots = {
EquipmentIndex.Weapon0,
EquipmentIndex.Weapon1,
EquipmentIndex.Weapon2,
EquipmentIndex.Weapon3
};
private static int _counter;
private readonly ReaderWriterLockSlim _eqLock = new();
private readonly ReaderWriterLockSlim _lock = new();
public readonly Equipment Equipment;
private bool _isAssigned;
public Assignment(CharacterObject character) {
_lock.EnterWriteLock();
try { Index = Interlocked.Increment(ref _counter); }
finally { _lock.ExitWriteLock(); }
Character = character;
Equipment = CreateEmptyEquipment();
ReferenceEquipment = character.RandomBattleEquipment.Clone();
}
public int Index { get; }
public bool IsAssigned
{
get
{
//return _isAssigned;
_lock.EnterReadLock();
try { return _isAssigned; }
finally { _lock.ExitReadLock(); }
}
set
{
//_isAssigned = value;
_lock.EnterWriteLock();
try { _isAssigned = value; }
finally { _lock.ExitWriteLock(); }
}
}
public CharacterObject Character { get; }
public Equipment ReferenceEquipment { get; }
public bool IsShielded =>
WeaponSlots.AnyQ(slot => GetEquipmentFromSlot(slot) is {
IsEmpty : false,
Item.ItemType: ItemTypeEnum.Shield
});
public bool CanBeShielded =>
WeaponSlots.AnyQ(slot => GetEquipmentFromSlot(slot) is {
IsEmpty: false,
Item: {
ItemType: ItemTypeEnum.OneHandedWeapon
} item
} &&
!item.CantUseWithShields());
public bool IsArcher =>
WeaponSlots.AnyQ(slot => GetEquipmentFromSlot(slot) is { IsEmpty: false, Item: { } item } && item.IsBow());
public bool IsCrossBowMan =>
WeaponSlots.AnyQ(slot => GetEquipmentFromSlot(slot) is { IsEmpty: false, Item: { } item } && item.IsCrossBow());
public bool HaveThrown =>
WeaponSlots.AnyQ(slot => GetEquipmentFromSlot(slot) is { IsEmpty: false, Item: { } item } && item.IsThrowing());
public bool HaveTwoHandedWeaponOrPolearms =>
WeaponSlots.AnyQ(slot => GetEquipmentFromSlot(slot) is { IsEmpty: false, Item: { } item } && (item.IsTwoHanded() || item.IsPolearm()));
public EquipmentIndex? EmptyWeaponSlot
{
get
{
foreach (var slot in WeaponSlots)
if (GetEquipmentFromSlot(slot).IsEmpty || GetEquipmentFromSlot(slot).Item == null)
return slot;
return null;
}
}
public bool IsMounted
{
get
{
var horse = ReferenceEquipment.GetEquipmentFromSlot(EquipmentIndex.Horse);
return horse is { IsEmpty: false, Item: not null };
}
}
public bool IsUnarmed =>
(GetEquipmentFromSlot(EquipmentIndex.Weapon0).IsEmpty || GetEquipmentFromSlot(EquipmentIndex.Weapon0).Item == null) &&
(GetEquipmentFromSlot(EquipmentIndex.Weapon1).IsEmpty || GetEquipmentFromSlot(EquipmentIndex.Weapon1).Item == null) &&
(GetEquipmentFromSlot(EquipmentIndex.Weapon2).IsEmpty || GetEquipmentFromSlot(EquipmentIndex.Weapon2).Item == null) &&
(GetEquipmentFromSlot(EquipmentIndex.Weapon3).IsEmpty || GetEquipmentFromSlot(EquipmentIndex.Weapon3).Item == null);
public int CompareTo(object? obj) {
if (obj == null) return 1;
if (obj is not Assignment other) throw new ArgumentException("Object is not an Assignment");
var tierComparison = Character.Tier.CompareTo(other.Character.Tier);
if (tierComparison != 0) return tierComparison;
// TroopType 比较,小的放在前面
var thisTroopType = Character.GetTroopType();
var otherTroopType = other.Character.GetTroopType();
var troopTypeComparison = thisTroopType.CompareTo(otherTroopType);
if (troopTypeComparison != 0) return troopTypeComparison;
// 按 IsRanged 比较,IsRanged 排在 !IsRanged 前面
if (!Character.IsRanged && other.Character.IsRanged) return 1;
if (Character.IsRanged && !other.Character.IsRanged) return -1;
// 按 IsMounted 比较,!IsMounted 排在 IsMounted 前面
if (Character.IsMounted && !other.Character.IsMounted) return 1;
if (!Character.IsMounted && other.Character.IsMounted) return -1;
var skillValueComparison = Character.SkillValue().CompareTo(other.Character.SkillValue());
if (skillValueComparison != 0) return skillValueComparison;
var equipmentValueComparison = Character.EquipmentValue().CompareTo(other.Character.EquipmentValue());
if (equipmentValueComparison != 0) return equipmentValueComparison;
var levelComparison = Character.Level.CompareTo(other.Character.Level);
if (levelComparison != 0) return levelComparison;
return 0; // 如果所有条件都相等,则认为两者相等
}
public EquipmentElement GetEquipmentFromSlot(EquipmentIndex slot) {
_eqLock.EnterReadLock();
try { return Equipment.GetEquipmentFromSlot(slot); }
finally { _eqLock.ExitReadLock(); }
}
public void SetEquipment(EquipmentIndex slot, EquipmentElement equipment) {
_eqLock.EnterWriteLock();
try { Equipment.AddEquipmentToSlotWithoutAgent(slot, equipment); }
finally { _eqLock.ExitWriteLock(); }
}
private static Equipment CreateEmptyEquipment() {
Equipment emptyEquipment = new();
foreach (var slot in Global.EquipmentSlots)
emptyEquipment.AddEquipmentToSlotWithoutAgent(slot, new EquipmentElement());
return emptyEquipment;
}
public void FillEmptySlots() {
foreach (var slot in Global.ArmourSlots) {
var referenceEquipment = ReferenceEquipment.GetEquipmentFromSlot(slot);
if (GetEquipmentFromSlot(slot) is not { IsEmpty: false, Item: not null }) {
var itemType = Helper.EquipmentIndexToItemEnumType(slot);
if (!itemType.HasValue) continue;
ItemObject? item;
if (referenceEquipment is { IsEmpty: false, Item: not null }) {
List<ItemObject> itemList = new() { referenceEquipment.Item };
var itemsByCharacter = Cache.GetItemsByTypeTierAndCulture(itemType.Value, Character.Tier, Character.Culture);
if (itemsByCharacter != null) itemList.AddRange(itemsByCharacter);
if (referenceEquipment.Item.Culture is CultureObject cultureObject) {
var itemsByReference = Cache.GetItemsByTypeTierAndCulture(itemType.Value, (int)referenceEquipment.Item.Tier, cultureObject);
if (itemsByReference != null) itemList.AddRange(itemsByReference);
}
item = WeightedRandomSelector.SelectItem(itemList, referenceEquipment.Item.Effectiveness);
}
else {
item = Cache.GetItemsByTypeTierAndCulture(itemType.Value, Character.Tier, Character.Culture)?.GetRandomElement();
if (item == null) continue;
}
SetEquipment(slot, new EquipmentElement(item));
}
}
foreach (var slot in Global.EquipmentSlots)
if (GetEquipmentFromSlot(slot) is not { IsEmpty: false, Item: not null })
SetEquipment(slot, ReferenceEquipment.GetEquipmentFromSlot(slot));
}
}