forked from Katarn2000x/DiplomacyFixes
-
Notifications
You must be signed in to change notification settings - Fork 6
/
CooldownManager.cs
188 lines (163 loc) · 7.12 KB
/
CooldownManager.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
using DiplomacyFixes.DiplomaticAction.NonAggressionPact;
using System;
using System.Collections.Generic;
using System.Linq;
using TaleWorlds.CampaignSystem;
using TaleWorlds.ObjectSystem;
using TaleWorlds.SaveSystem;
namespace DiplomacyFixes
{
[SaveableClass(3)]
class CooldownManager
{
internal static Dictionary<string, CampaignTime> LastWarTime { get; private set; }
internal static Dictionary<Kingdom, CampaignTime> LastPeaceProposalTime { get; private set; }
internal static Dictionary<string, CampaignTime> LastAllianceFormedTime { get; private set; }
[SaveableField(1)]
private Dictionary<string, CampaignTime> _lastWarTime;
[SaveableField(2)]
private Dictionary<Kingdom, CampaignTime> _lastPeaceProposalTime;
[SaveableField(3)]
private Dictionary<string, CampaignTime> _lastAllianceFormedTime;
private static float MinimumDaysBetweenPeaceProposals { get { return 5f; } }
internal CooldownManager()
{
_lastWarTime = new Dictionary<string, CampaignTime>();
_lastPeaceProposalTime = new Dictionary<Kingdom, CampaignTime>();
_lastAllianceFormedTime = new Dictionary<string, CampaignTime>();
LastWarTime = _lastWarTime;
LastPeaceProposalTime = _lastPeaceProposalTime;
LastAllianceFormedTime = _lastAllianceFormedTime;
}
public void UpdateLastPeaceProposalTime(Kingdom kingdom, CampaignTime campaignTime)
{
_lastPeaceProposalTime[kingdom] = campaignTime;
}
public void UpdateLastAllianceFormedTime(Kingdom kingdom1, Kingdom kingdom2, CampaignTime campaignTime)
{
string key = CreateKey(kingdom1, kingdom2);
_lastAllianceFormedTime[key] = campaignTime;
}
public static bool HasBreakAllianceCooldown(Kingdom kingdom1, Kingdom kingdom2, out float elapsedDaysUntilNow)
{
bool hasBreakAllianceCooldown = false;
if (LastAllianceFormedTime.TryGetValue(CreateKey(kingdom1, kingdom2), out CampaignTime value))
{
hasBreakAllianceCooldown = (elapsedDaysUntilNow = value.ElapsedDaysUntilNow) < Settings.Instance.MinimumAllianceDuration;
}
else
{
elapsedDaysUntilNow = default;
}
return hasBreakAllianceCooldown;
}
private static string CreateKey(IFaction faction1, IFaction faction2)
{
List<IFaction> factions = new List<IFaction> { faction1, faction2 };
IEnumerable<string> keyArguments = factions.OrderBy(faction => faction.StringId).Select(faction => faction.StringId);
return string.Join("+", keyArguments);
}
private static bool DecodeKeyToKingdoms(string key, out Tuple<Kingdom, Kingdom> kingdoms)
{
Kingdom faction1, faction2;
string[] factionStringIds = key.Split('+');
faction1 = MBObjectManager.Instance.GetObject<Kingdom>(factionStringIds[0]);
faction2 = MBObjectManager.Instance.GetObject<Kingdom>(factionStringIds[1]);
if (faction1 != null && faction2 != null)
{
kingdoms = Tuple.Create(faction1, faction2);
return true;
}
else
{
kingdoms = default;
return false;
}
}
public static CampaignTime? GetLastWarTimeBetweenFactions(IFaction faction1, IFaction faction2)
{
if (LastWarTime.TryGetValue(CreateKey(faction1, faction2), out CampaignTime value))
{
return value;
}
else
{
return default;
}
}
public static bool HasDeclareWarCooldown(IFaction faction1, IFaction faction2, out float elapsedTime)
{
CampaignTime? campaignTime = GetLastWarTimeBetweenFactions(faction1, faction2);
if (campaignTime.HasValue)
{
elapsedTime = campaignTime.Value.ElapsedDaysUntilNow;
return campaignTime.Value.ElapsedDaysUntilNow < Settings.Instance.DeclareWarCooldownInDays;
}
else
{
elapsedTime = default;
return false;
}
}
public static bool HasPeaceProposalCooldownWithPlayerKingdom(Kingdom kingdom)
{
return GetLastPeaceProposalTime(kingdom).HasValue && GetLastPeaceProposalTime(kingdom).Value.ElapsedDaysUntilNow < MinimumDaysBetweenPeaceProposals;
}
public static bool HasPeaceProposalCooldown(Kingdom kingdomProposingPeace, Kingdom otherKingdom)
{
return (!HasExceededMinimumWarDuration(kingdomProposingPeace, otherKingdom, out float elapsedTime)
|| (otherKingdom.Leader.IsHumanPlayerCharacter && HasPeaceProposalCooldownWithPlayerKingdom(kingdomProposingPeace)));
}
public static bool HasExceededMinimumWarDuration(IFaction faction1, IFaction faction2, out float elapsedTime)
{
elapsedTime = -1f;
StanceLink stanceLink = faction1.GetStanceWith(faction2);
if (stanceLink?.IsAtWar ?? false)
{
elapsedTime = stanceLink.WarStartDate.ElapsedDaysUntilNow;
int minimumWarDurationInDays = Settings.Instance.MinimumWarDurationInDays;
return elapsedTime >= minimumWarDurationInDays;
}
return true;
}
public static CampaignTime? GetLastWarTimeWithPlayerFaction(IFaction faction)
{
return GetLastWarTimeBetweenFactions(faction, Hero.MainHero.MapFaction);
}
public static CampaignTime? GetLastPeaceProposalTime(Kingdom kingdom)
{
if (LastPeaceProposalTime.TryGetValue(kingdom, out CampaignTime value))
{
return value;
}
return default;
}
internal void Sync()
{
if (_lastWarTime == null)
{
_lastWarTime = new Dictionary<string, CampaignTime>();
}
if (_lastPeaceProposalTime == null)
{
_lastPeaceProposalTime = new Dictionary<Kingdom, CampaignTime>();
}
if (_lastAllianceFormedTime == null)
{
_lastAllianceFormedTime = new Dictionary<string, CampaignTime>();
}
LastWarTime = _lastWarTime;
LastPeaceProposalTime = _lastPeaceProposalTime;
LastAllianceFormedTime = _lastAllianceFormedTime;
foreach (string key in _lastWarTime.Keys)
{
float duration = Settings.Instance.DeclareWarCooldownInDays - _lastWarTime[key].ElapsedDaysUntilNow;
if (duration > 0 & DecodeKeyToKingdoms(key, out Tuple<Kingdom, Kingdom> kingdoms))
{
FormNonAggressionPactAction.Apply(kingdoms.Item1, kingdoms.Item2, bypassCosts: true, customDurationInDays: duration, queryPlayer: false);
}
}
_lastWarTime.Clear();
}
}
}