-
Notifications
You must be signed in to change notification settings - Fork 0
/
PopulationControlCampaignBehavior.cs
278 lines (251 loc) · 10.7 KB
/
PopulationControlCampaignBehavior.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
using Helpers;
using System;
using System.Collections.Generic;
using TaleWorlds.CampaignSystem;
using TaleWorlds.CampaignSystem.GameMenus;
using TaleWorlds.CampaignSystem.Overlay;
using TaleWorlds.Core;
using TaleWorlds.Localization;
namespace PopulationControl
{
class PopulationControlCampaignBehavior : CampaignBehaviorBase
{
private const string PROSPERITY_CHEAT_MENU_TITLE =
"{=!}Change Prosperity (Cheat)";
private const string MANAGE_POPULATION_MENU_TITLE = "{=!}Manage Population";
private const float MIN_PROSPERITY = 1.1f;
private const float MIN_LOYALTY = 0.0f;
private const float LOYALTY_RATIO = 1.0f;
private const int MENU_TOWN_INSERT_INDEX = 5;
private const int MENU_CASTLE_INSERT_INDEX = 3;
private const string MODULE_ID = PopulationControlSubModule.MODULE_ID;
public override void RegisterEvents()
{
CampaignEvents.OnNewGameCreatedEvent.AddNonSerializedListener(
this, new Action<CampaignGameStarter>(OnAfterNewGameCreated));
CampaignEvents.OnGameLoadedEvent.AddNonSerializedListener(
this, new Action<CampaignGameStarter>(OnAfterNewGameCreated));
}
public void OnAfterNewGameCreated(CampaignGameStarter campaignGameStarter)
{
AddGameMenus(campaignGameStarter);
}
private void AddGameMenus(CampaignGameStarter campaignGameSystemStarter)
{
try
{
AddManagePopulationToMenu(
new List<Tuple<string, int>>{
new Tuple<string, int>("town", MENU_TOWN_INSERT_INDEX),
new Tuple<string, int>("castle", MENU_CASTLE_INSERT_INDEX)},
campaignGameSystemStarter);
AddProsperityCheatToMenu(
new List<Tuple<string, int>>{
new Tuple<string, int>("town", MENU_TOWN_INSERT_INDEX + 1),
new Tuple<string, int>("castle", MENU_CASTLE_INSERT_INDEX + 1)},
campaignGameSystemStarter);
}
catch (KeyNotFoundException)
{
DisplayInfoMsg(
"Population Control mod: Couldn't add menus. This is harmless, but you'll need to reload your save before the population management menus appear.");
}
}
private void
AddManagePopulationToMenu(IEnumerable<Tuple<string, int>> parentMenus,
CampaignGameStarter campaignGameSystemStarter)
{
foreach (Tuple<string, int> parentMenu in parentMenus)
{
AddManagePopulationToMenu(parentMenu, campaignGameSystemStarter);
}
}
private void
AddManagePopulationToMenu(Tuple<string, int> parentMenu,
CampaignGameStarter campaignGameSystemStarter)
{
string parentMenuId = parentMenu.Item1;
int insertIntoParentAt = parentMenu.Item2;
string menuId = MODULE_ID + "_" + parentMenuId + "_manage_population";
campaignGameSystemStarter.AddGameMenuOption(
parentMenuId, menuId, MANAGE_POPULATION_MENU_TITLE,
new GameMenuOption.OnConditionDelegate(
game_menu_manage_population_on_condition),
x => GameMenu.SwitchToMenu(menuId), false, insertIntoParentAt);
const float prosperityRatioDisplay = 0.1f;
const float loyaltyRatioDisplay = prosperityRatioDisplay * LOYALTY_RATIO;
campaignGameSystemStarter.AddGameMenu(
menuId,
"{=!}Manage Population.\nPopulation is considered to be equivalent to " +
"prosperity and reducing it reduces loyalty proportionally. E.g. if " +
"you remove 500 people and this is " +
(prosperityRatioDisplay * 100.0f).ToString() + "% of them, " +
(loyaltyRatioDisplay * 100.0f).ToString() +
"% of loyalty will also be reduced.",
new OnInitDelegate(game_menu_manage_population_on_init),
GameOverlays.MenuOverlayType.SettlementWithCharacters);
campaignGameSystemStarter.AddGameMenuOption(
menuId, menuId + "_decrease_10_percent",
"{=!}Kick 10% of people out (decreases loyalty)", x => true,
new GameMenuOption.OnConsequenceDelegate(
game_menu_manage_population_decrease_pop_10_percent_on_consequence));
campaignGameSystemStarter.AddGameMenuOption(
menuId, menuId + "_decrease_30_percent",
"{=!}Kick 30% of people out (decreases loyalty)", x => true,
new GameMenuOption.OnConsequenceDelegate(
game_menu_manage_population_decrease_pop_30_percent_on_consequence));
campaignGameSystemStarter.AddGameMenuOption(
menuId, menuId + "_decrease_200",
"{=!}Kick 200 people out (decreases loyalty)", x => true,
new GameMenuOption.OnConsequenceDelegate(
game_menu_manage_population_decrease_pop_200_on_consequence));
campaignGameSystemStarter.AddGameMenuOption(
menuId, menuId + "_decrease_500",
"{=!}Kick 500 people out (decreases loyalty)", x => true,
new GameMenuOption.OnConsequenceDelegate(
game_menu_manage_population_decrease_pop_500_on_consequence));
campaignGameSystemStarter.AddGameMenuOption(
menuId, menuId + "_change_back", "{=!}Done",
new GameMenuOption.OnConditionDelegate(back_on_condition),
x => GameMenu.SwitchToMenu(parentMenuId), true);
}
private void
AddProsperityCheatToMenu(IEnumerable<Tuple<string, int>> parentMenus,
CampaignGameStarter campaignGameSystemStarter)
{
foreach (Tuple<string, int> parentMenu in parentMenus)
{
AddProsperityCheatToMenu(parentMenu, campaignGameSystemStarter);
}
}
private void
AddProsperityCheatToMenu(Tuple<string, int> parentMenu,
CampaignGameStarter campaignGameSystemStarter)
{
string parentMenuId = parentMenu.Item1;
int insertIntoParentAt = parentMenu.Item2;
string menuId = MODULE_ID + "_" + parentMenuId + "_prosperity_cheat";
campaignGameSystemStarter.AddGameMenuOption(
parentMenuId, menuId, PROSPERITY_CHEAT_MENU_TITLE,
new GameMenuOption.OnConditionDelegate(
game_menu_prosperity_cheat_on_condition),
x => GameMenu.SwitchToMenu(menuId), false, insertIntoParentAt);
campaignGameSystemStarter.AddGameMenu(
menuId, PROSPERITY_CHEAT_MENU_TITLE,
new OnInitDelegate(game_menu_prosperity_cheat_on_init),
GameOverlays.MenuOverlayType.SettlementWithCharacters);
campaignGameSystemStarter.AddGameMenuOption(
menuId, menuId + "_increase_500", "{=!}Increase by 500 (no penalty)",
x => true,
new GameMenuOption.OnConsequenceDelegate(
game_menu_prosperity_cheat_increase_prosperity_by_500_on_consequence));
campaignGameSystemStarter.AddGameMenuOption(
menuId, menuId + "_decrease_500", "{=!}Decrease by 500 (no penalty)",
x => true,
new GameMenuOption.OnConsequenceDelegate(
game_menu_prosperity_cheat_decrease_prosperity_by_500_on_consequence));
campaignGameSystemStarter.AddGameMenuOption(
menuId, menuId + "_change_back", "{=!}Done",
new GameMenuOption.OnConditionDelegate(back_on_condition),
x => GameMenu.SwitchToMenu(parentMenuId), true);
}
private static bool
game_menu_manage_population_on_condition(MenuCallbackArgs args)
{
return Settlement.CurrentSettlement.OwnerClan.Leader == Hero.MainHero;
}
private static void
game_menu_manage_population_on_init(MenuCallbackArgs args)
{
Campaign.Current.GameMenuManager.MenuLocations.Clear();
}
private static void
game_menu_manage_population_decrease_pop_10_percent_on_consequence(
MenuCallbackArgs args)
{
ChangeProsperityPercent(-0.1f);
}
private static void
game_menu_manage_population_decrease_pop_30_percent_on_consequence(
MenuCallbackArgs args)
{
ChangeProsperityPercent(-0.3f);
}
private static void
game_menu_manage_population_decrease_pop_200_on_consequence(
MenuCallbackArgs args)
{
ChangeProsperity(-200.0f);
}
private static void
game_menu_manage_population_decrease_pop_500_on_consequence(
MenuCallbackArgs args)
{
ChangeProsperity(-500.0f);
}
private static bool
game_menu_prosperity_cheat_on_condition(MenuCallbackArgs args)
{
return Game.Current.CheatMode;
}
private static void
game_menu_prosperity_cheat_on_init(MenuCallbackArgs args)
{
Campaign.Current.GameMenuManager.MenuLocations.Clear();
}
private static void
game_menu_prosperity_cheat_increase_prosperity_by_500_on_consequence(
MenuCallbackArgs args)
{
ChangeProsperityRaw(500.0f);
}
private static void
game_menu_prosperity_cheat_decrease_prosperity_by_500_on_consequence(
MenuCallbackArgs args)
{
ChangeProsperityRaw(-500.0f);
}
private static bool back_on_condition(MenuCallbackArgs args)
{
args.optionLeaveType = GameMenuOption.LeaveType.Leave;
return true;
}
private static void ChangeProsperityPercent(float deltaPercent)
{
ChangeProsperity(Settlement.CurrentSettlement.Prosperity * deltaPercent);
}
private static void ChangeProsperityRaw(float delta)
{
Settlement.CurrentSettlement.Prosperity = Math.Max(
MIN_PROSPERITY, Settlement.CurrentSettlement.Prosperity + delta);
DisplayInfoMsg("Prosperity has been changed to " +
Settlement.CurrentSettlement.Prosperity.ToString() +
" in " + Settlement.CurrentSettlement.Name.ToString());
}
private static void ChangeProsperity(float delta)
{
float oldProsperity = Settlement.CurrentSettlement.Prosperity;
float newProsperity = Math.Max(MIN_PROSPERITY, oldProsperity + delta);
float realProsperityDelta = newProsperity - oldProsperity;
ChangeProsperityRaw(realProsperityDelta);
if (Settlement.CurrentSettlement.IsTown ||
Settlement.CurrentSettlement.IsCastle)
{
// no loyalty change on increase in population
float loyaltyDeltaRatio =
Math.Min(0.0f, realProsperityDelta / oldProsperity * LOYALTY_RATIO);
float oldLoyalty = Settlement.CurrentSettlement.Town.Loyalty;
Settlement.CurrentSettlement.Town.Loyalty =
Math.Max(MIN_LOYALTY, oldLoyalty + oldLoyalty * loyaltyDeltaRatio);
DisplayInfoMsg("Loyalty has been changed to " +
Settlement.CurrentSettlement.Town.Loyalty + " in " +
Settlement.CurrentSettlement.Name.ToString());
}
}
private static void DisplayInfoMsg(string msg)
{
InformationManager.DisplayMessage(new InformationMessage(msg));
}
public override void SyncData(IDataStore dataStore) {}
}
}