-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathArmory.cs
177 lines (150 loc) · 5.09 KB
/
Armory.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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using Bannerlord.DynamicTroop.Patches;
using TaleWorlds.CampaignSystem.Party;
using TaleWorlds.CampaignSystem.Roster;
using TaleWorlds.Core;
using TaleWorlds.ObjectSystem;
namespace Bannerlord.DynamicTroop;
[Obsolete]
public class Armory {
private readonly ConcurrentDictionary<EquipmentElement, int> _dict;
private MobileParty _party;
public Armory(MobileParty party) {
_dict = new ConcurrentDictionary<EquipmentElement, int>();
Party = party;
}
public Armory(MobileParty party, Dictionary<(uint, uint), int> loadedData) {
_dict = new ConcurrentDictionary<EquipmentElement, int>();
try {
foreach (var kvp in loadedData) {
var itemId = kvp.Key.Item1;
var modifierId = kvp.Key.Item2;
var count = kvp.Value;
var itemObj = MBObjectManager.Instance.GetObject(new MBGUID(itemId));
var modifierObj = MBObjectManager.Instance.GetObject(new MBGUID(modifierId));
if (itemObj == null || count <= 0) continue;
var item = (ItemObject)itemObj;
ItemModifier? modifier = null;
if (modifierObj != null) modifier = (ItemModifier)modifierObj;
EquipmentElement equipmentElement = new(item, modifier);
AddEquipmentElement(equipmentElement, count);
}
}
catch (Exception e) { Global.Error(e.Message); }
_party = party;
}
public Armory(MobileParty party, ItemRoster itemRoster) {
_dict = new ConcurrentDictionary<EquipmentElement, int>();
ItemRoster = itemRoster;
_party = party;
}
public MobileParty Party {
get => _party;
private set {
try {
_party = value;
var troopRoster = _party.MemberRoster?.GetTroopRoster();
if (troopRoster == null) return;
foreach (var troopRosterElement in troopRoster) {
if (troopRosterElement.Number <= 0) continue;
var character = troopRosterElement.Character;
if (character == null) continue;
var equipmentList = RecruitmentPatch.GetRecruitEquipments(character);
foreach (var equipment in equipmentList) {
if (equipment is not { IsEmpty: false, Item: not null }) continue;
AddEquipmentElement(equipment, troopRosterElement.Number);
}
}
}
catch (Exception e) { Global.Error(e.Message); }
}
}
public ItemRoster ItemRoster {
get {
ItemRoster roster = new();
try {
foreach (var kvp in _dict)
if (kvp.Key.Item != null && kvp.Value > 0)
_ = roster.AddToCounts(kvp.Key, kvp.Value);
}
catch (Exception e) { Global.Error(e.Message); }
return roster;
}
set {
try {
_dict.Clear();
foreach (var item in value)
if (item.EquipmentElement is { IsEmpty: false, Item: not null })
AddEquipmentElement(item.EquipmentElement, item.Amount);
}
catch (Exception e) { Global.Error(e.Message); }
}
}
public Dictionary<(uint, uint), int> GenerateDataForSave() {
Dictionary<(uint, uint), int> data = new();
try {
foreach (var kvp in _dict) {
var item = kvp.Key.Item;
var modifier = kvp.Key.ItemModifier;
var count = kvp.Value;
if (item == null || count <= 0) continue;
data.Add((item.Id.InternalValue, modifier.Id.InternalValue), count);
}
}
catch (Exception e) { Global.Error(e.Message); }
return data;
}
public void AddItem(ItemObject item, int count = 1) {
try { AddEquipmentElement(new EquipmentElement(item), count); }
catch (Exception e) { Global.Error(e.Message); }
}
public void AddEquipmentElement(EquipmentElement equipmentElement, int count = 1) {
try {
if (equipmentElement is { IsEmpty: false, Item: not null })
_ = _dict.AddOrUpdate(equipmentElement, count, (_, v) => v + count);
}
catch (Exception e) { Global.Error(e.Message); }
}
public void RemoveItem(ItemObject item, int cnt = 1) {
try {
ConcurrentDictionary<EquipmentElement, int> toRemove = new();
foreach (var kvp in _dict)
if (kvp.Key.Item == item) {
var cntToRemove = Math.Min(cnt, kvp.Value);
_ = toRemove.AddOrUpdate(kvp.Key, cntToRemove, (_, v) => v + cntToRemove);
cnt -= cntToRemove;
if (cnt <= 0) break;
}
foreach (var kvp in toRemove) RemoveEquipmentElement(kvp.Key, kvp.Value);
}
catch (Exception e) { Global.Error(e.Message); }
}
public void RemoveEquipmentElement(EquipmentElement equipmentElement, int cnt = 1) {
try {
if (equipmentElement is { IsEmpty: false, Item: not null })
if (_dict.ContainsKey(equipmentElement)) {
if (_dict[equipmentElement] < cnt) Global.Warn("greater than");
_dict[equipmentElement] -= cnt;
if (_dict[equipmentElement] <= 0) _ = _dict.TryRemove(equipmentElement, out _);
}
}
catch (Exception e) { Global.Error(e.Message); }
}
public bool HaveEquipmentElement(EquipmentElement equipmentElement) {
try { return _dict.ContainsKey(equipmentElement) && _dict[equipmentElement] > 0; }
catch (Exception e) {
Global.Error(e.Message);
return false;
}
}
public bool HaveItem(ItemObject item) {
try { return _dict.Any(kvp => kvp.Key.Item == item && kvp.Value > 0); }
catch (Exception e) {
Global.Error(e.Message);
return false;
}
}
}