-
Notifications
You must be signed in to change notification settings - Fork 1
/
ModBanditMilitiaPartyComponent.cs
74 lines (67 loc) · 2.9 KB
/
ModBanditMilitiaPartyComponent.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
using System.Collections.Generic;
using System.Reflection;
using HarmonyLib;
using TaleWorlds.CampaignSystem;
using TaleWorlds.CampaignSystem.Party.PartyComponents;
using TaleWorlds.CampaignSystem.Settlements;
using TaleWorlds.Core;
using TaleWorlds.Library;
using TaleWorlds.Localization;
using TaleWorlds.SaveSystem;
using static BanditMilitias.Globals;
using static BanditMilitias.Helper;
// ReSharper disable ConvertToAutoProperty
// ReSharper disable InconsistentNaming
namespace BanditMilitias
{
public class ModBanditMilitiaPartyComponent : WarPartyComponent
{
[SaveableField(1)] public readonly Banner Banner;
[SaveableField(2)] public readonly string BannerKey;
[SaveableField(3)] public CampaignTime LastMergedOrSplitDate = CampaignTime.Now;
[SaveableField(4)] public Dictionary<Hero, float> Avoidance = new();
[SaveableField(5)] private Hero leader;
[SaveableField(6)] private Settlement homeSettlement;
[CachedData] private TextObject cachedName;
public override Settlement HomeSettlement => homeSettlement;
public override Hero Leader => leader;
public override Hero PartyOwner => MobileParty?.ActualClan?.Leader; // clan is null during nuke
private static readonly MethodInfo GetLocalizedText = AccessTools.Method(typeof(MBTextManager), "GetLocalizedText");
private static readonly MethodInfo OnWarPartyRemoved = AccessTools.Method(typeof(Clan), "OnWarPartyRemoved");
public override TextObject Name
{
get
{
cachedName ??= new TextObject((string)GetLocalizedText.Invoke(
null, new object[] { $"{Possess(Leader.FirstName.ToString())} {Globals.Settings.BanditMilitiaString}" }));
cachedName.SetTextVariable("IS_BANDIT", 1);
return cachedName;
}
}
public override void ChangePartyLeader(Hero newLeader)
{
Traverse.Create(this).Field<Hero>("<Leader>k__BackingField").Value = newLeader;
if (newLeader != null && Leader != newLeader && !Leader.IsDead)
Leader?.RemoveMilitiaHero();
}
protected override void OnInitialize()
{
base.OnInitialize();
if (!IsBandit(MobileParty))
IsBandit(MobileParty) = true;
OnWarPartyRemoved.Invoke(Clan, new[] { this });
}
public ModBanditMilitiaPartyComponent(Clan heroClan)
{
Banner = Banners.GetRandomElement();
BannerKey = Banner.Serialize();
var hero = CreateHero(heroClan);
if (hero.HomeSettlement is null)
_bornSettlement(hero) = Hideouts.GetRandomElement();
hero.UpdateHomeSettlement();
HiddenInEncyclopedia(hero.CharacterObject) = true;
homeSettlement = hero.BornSettlement;
leader = hero;
}
}
}