-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cs
237 lines (222 loc) · 11.8 KB
/
Main.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using KingdomTemplate.Models;
using TaleWorlds.CampaignSystem;
using TaleWorlds.CampaignSystem.SandBox;
using TaleWorlds.Core;
using TaleWorlds.Engine.GauntletUI;
using TaleWorlds.Engine.Screens;
using TaleWorlds.InputSystem;
using TaleWorlds.Library;
using TaleWorlds.Localization;
using TaleWorlds.MountAndBlade;
namespace KingdomTemplate
{
public class Main : MBSubModuleBase
{
private static List<KingdomConfig> KingdomConfigs = new List<KingdomConfig>();
private static string baseXDocPath = BasePath.Name + "Modules\\KingdomTemplate\\ModuleData\\";
private static string configXDocPath = baseXDocPath + "config.xml";
private KingdomTemplateBehavior _kingdomTemplateBehavior;
protected override void OnBeforeInitialModuleScreenSetAsRoot() // Main Menu.
{
base.OnBeforeInitialModuleScreenSetAsRoot();
Initialize();
}
protected override void OnGameStart(Game game, IGameStarter gameStarterObject)
{
base.OnGameStart(game, gameStarterObject);
if (!(game.GameType is Campaign))
return;
// Here we create a new custom campaign behavior, KingdomTemplateBehavior, and add it to the CampaignGameStarter.
CampaignGameStarter campaignGameStarter = (CampaignGameStarter) gameStarterObject;
_kingdomTemplateBehavior = new KingdomTemplateBehavior(new DataSynchronizer());
campaignGameStarter.AddBehavior(_kingdomTemplateBehavior);
}
private void Initialize()
{
KingdomConfigs.Clear();
// Load mod's config information, this defines what to look for when loading the mods other xmls
if (File.Exists(configXDocPath))
{
try
{
XDocument doc = XDocument.Load(configXDocPath);
if (doc.Root != null)
{
IEnumerable<XElement> kingdomConfigs = doc.Root.Elements();
if (kingdomConfigs.Any())
{
IEnumerator<XElement> elementrator = kingdomConfigs.GetEnumerator();
while (elementrator.MoveNext())
{
XElement current = elementrator.Current;
if (current?.Element("KingdomId") != null &&
current.Element("LocationsFileName") != null &&
current.Element("KingdomLordCollection") != null &&
current.Element("KingdomLord") != null &&
current.Element("kingdomLeaderId") != null)
{
KingdomConfig kingdomConfig = new KingdomConfig(
current.Element("KingdomId")?.Value,
current.Element("LocationsFileName")?.Value,
current.Element("KingdomLordCollection")?.Value,
current.Element("KingdomLord")?.Value,
current.Element("kingdomLeaderId")?.Value);
KingdomConfigs.Add(kingdomConfig);
}
}
}
}
}
catch (Exception e)
{
InformationManager.DisplayMessage(new InformationMessage("KingdomTemplate: Config.xml Data: " + e.Message, Color.FromUint(4278255360U)));
}
}
else
{
InformationManager.DisplayMessage(new InformationMessage("KingdomTemplate: Config.xml not found", Color.FromUint(4278255360U)));
}
if (KingdomConfigs.Any())
{
KingdomConfigs.ForEach(kC =>
{
// if (File.Exists(locationsxDocPath))
// {
// try
// {
// XDocument doc = XDocument.Load(locationsxDocPath);
// if (doc.Root != null)
// {
// IEnumerable<XElement> headhunters = doc.Root.Elements("Headhunters").Elements("Headhunter");
// IEnumerable<XElement> elements = headhunters.ToList();
//
// if (elements.Any())
// {
// IEnumerator<XElement> elementrator = elements.GetEnumerator();
// while (elementrator.MoveNext())
// {
// XElement current = elementrator.Current;
// if (current?.Element("Name") != null &&
// current.Element("Settlement") != null)
// {
// string heroName = current.Element("Name")?.Value;
// string heroLocation = current.Element("Settlement")?.Value;
// hunterLocations.Add(heroName, heroLocation);
// }
// }
// }
// }
// }
// catch (Exception e)
// {
// InformationManager.DisplayMessage(new InformationMessage("KingdomTemplate: Failed to load Hunter Location Data: " + e.Message, Color.FromUint(4278255360U)));
// }
// }
// InformationManager.DisplayMessage(new InformationMessage("KingdomTemplate: Initialized.", Color.FromUint(4282569842U)));
});
}
}
public static void SetupKingdomParties(MBReadOnlyList<MobileParty> mobileParties)
{
if(mobileParties == null)
{
return;
}
Dictionary<string, MobileParty> existingParties = new Dictionary<string, MobileParty>();
List<MobileParty> tempList = mobileParties.ToList();
tempList.ForEach(p =>
{
// Campaign.Current.MobileParties can have some invalid entries, make sure only valid MobileParties get added for processing
if (p.Leader?.Name != null && !existingParties.ContainsKey(p.Leader.Name.ToString()))
{
existingParties.Add(p.Leader.Name.ToString(), p);
}
});
if(existingParties.Count > 0 && KingdomConfigs.Any())
{
KingdomConfigs.ForEach(kC =>
{
Dictionary<string, KingdomLocation> lordLocations = LoadLocationsForKingdomConfiguration(kC);
if (!lordLocations.IsEmpty() && lordLocations.Count > 0)
{
string[] names = new string[lordLocations.Count];
lordLocations.Keys.CopyTo(names, 0);
List<string> lordsToAdd = names.ToList();
lordsToAdd.ForEach(lord =>
{
if (existingParties.ContainsKey(lord))
{
MobileParty p = existingParties[lord];
if (p?.Leader?.Name != null)
{
KingdomLocation location = lordLocations[lord];
Settlement targetSettlement = Settlement.Find(location.SettlementName);
// Here we assign ownership of the targetSettlement to the KingdomLeaderId defined in config.xml
if (p.Leader.StringId.Equals(kC.kingdomLeaderId))
{
TaleWorlds.CampaignSystem.Actions.ChangeOwnerOfSettlementAction.ApplyByDefault(p.LeaderHero, targetSettlement);
}
// Move the party's position, use the XOffset and YOffset from the relevant locations.xml
p.Position2D = new Vec2(targetSettlement.Position2D.X + location.XOffset, targetSettlement.Position2D.Y + location.YOffset);
// Make them patrol around their location
p.SetMovePatrolAroundSettlement(targetSettlement);
}
}
});
}
});
}
}
private static Dictionary<string, KingdomLocation> LoadLocationsForKingdomConfiguration(KingdomConfig config)
{
string fileLocation = baseXDocPath + config.locationFilename + ".xml";
Dictionary<string, KingdomLocation> lordLocations = new Dictionary<string, KingdomLocation>();
if (File.Exists(fileLocation))
{
try
{
XDocument doc = XDocument.Load(fileLocation);
if (doc.Root != null)
{
IEnumerable<XElement> lords = doc.Root.Elements(config.kingdomLordCollectionTag).Elements(config.kingdomLordTag);
IEnumerable<XElement> elements = lords.ToList();
if (elements.Any())
{
IEnumerator<XElement> elementrator = elements.GetEnumerator();
while (elementrator.MoveNext())
{
XElement current = elementrator.Current;
if (current?.Element("Name") != null &&
current.Element("Settlement") != null &&
current.Element("XOffset") != null &&
current.Element("YOffset") != null)
{
string heroName = current.Element("Name")?.Value;
string heroLocation = current.Element("Settlement")?.Value;
int xOffset = 0;
int yOffset = 0;
if (Int32.TryParse(current.Element("XOffset")?.Value, out xOffset) &&
Int32.TryParse(current.Element("YOffset")?.Value, out yOffset))
{
lordLocations.Add(heroName, new KingdomLocation(heroLocation, xOffset, yOffset));
}
}
}
}
}
}
catch (Exception e)
{
InformationManager.DisplayMessage(new InformationMessage("KingdomTemplate: Failed to load Lord Location Data: " + e.Message, Color.FromUint(4278255360U)));
}
}
return lordLocations;
}
}
}