-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSubModule.cs
More file actions
620 lines (541 loc) · 29.9 KB
/
SubModule.cs
File metadata and controls
620 lines (541 loc) · 29.9 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
using TaleWorlds.MountAndBlade;
using HarmonyLib;
using TaleWorlds.CampaignSystem;
using TaleWorlds.Core;
using Helpers;
using TaleWorlds.CampaignSystem.GameMenus;
using TaleWorlds.CampaignSystem.CampaignBehaviors;
using System;
using System.Collections;
using TaleWorlds.CampaignSystem.Party;
using TaleWorlds.CampaignSystem.Roster;
using TaleWorlds.CampaignSystem.Encounters;
using TaleWorlds.CampaignSystem.MapEvents;
using TaleWorlds.Library;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using TaleWorlds.CampaignSystem.Siege;
using TaleWorlds.Localization;
using System.Reflection;
using TaleWorlds.MountAndBlade.Source.Missions.Handlers;
using SandBox.View.Menu;
using TaleWorlds.CampaignSystem.TroopSuppliers;
using TaleWorlds.InputSystem;
namespace ChooseYourTroops
{
public class ChooseYourTroopsBehavior : CampaignBehaviorBase
{
private static BattleSideEnum _playerSide = BattleSideEnum.None;
private static TroopRoster? _actualTroopRoster = null;
private static List<(FlattenedTroopRosterElement, MapEventParty, float)> _actualReadyTroopsPriorityList = null;
private static bool _setupSide = false;
private static bool _haveChosenTroops = false;
private static int PlayerArmySize;
private static int EnemyArmySize;
private static int initialPlayerSpawn;
public override void RegisterEvents()
{
CampaignEvents.MissionTickEvent.AddNonSerializedListener(this, (float dt) =>
{
if (Input.IsKeyReleased(InputKey.H))
{
Mission mission = Mission.Current;
int trueAttackerAmount =
mission.AttackerTeam.ActiveAgents.Sum(agent => agent.Character.HasMount() ? 2 : 1);
int trueDefenderAmount =
mission.DefenderTeam.ActiveAgents.Sum(agent => agent.Character.HasMount() ? 2 : 1);
InformationManager.DisplayMessage(new InformationMessage(
new TextObject($"Attacker amount: {mission.AttackerTeam.ActiveAgents.Count} | true amount: {trueAttackerAmount}").ToString()));
InformationManager.DisplayMessage(new InformationMessage(
new TextObject($"Defender amount: {mission.DefenderTeam.ActiveAgents.Count} | true amount: {trueDefenderAmount}").ToString()));
InformationManager.DisplayMessage(new InformationMessage(
new TextObject($"All agents: {mission.AllAgents.Count}").ToString()));
}
});
CampaignEvents.OnMissionEndedEvent.AddNonSerializedListener(this, (IMission mission) => CheckTroopRoster());
CampaignEvents.OnGameLoadFinishedEvent.AddNonSerializedListener(this, CheckTroopRoster);
CampaignEvents.OnPartySizeChangedEvent.AddNonSerializedListener(this, (PartyBase party) =>
{
if (party == MobileParty.MainParty.Party && Mission.Current == null)
CheckTroopRoster();
});
}
public static bool DoesTroopCountByTwo(CharacterObject characterObject) => ChooseYourTroopsConfig.Instance is { SupportForMaximumTroops: true } && characterObject.HasMount();
private static void CheckTroopRoster()
{
if (_actualTroopRoster == null)
return;
var partyRoster = MobileParty.MainParty. MemberRoster. GetTroopRoster();
var actualRoster = _actualTroopRoster.GetTroopRoster();
_actualTroopRoster. Clear();
foreach (var actualTroop in actualRoster)
{
var partyTroop = partyRoster.FirstOrDefault(x =>
x.Character?. StringId == actualTroop. Character?.StringId);
if (partyTroop. Character == null ||
partyTroop. Number <= 0 ||
partyTroop.Number == partyTroop.WoundedNumber)
continue;
var healthyCount = partyTroop.Number - partyTroop.WoundedNumber;
var finalCount = Math.Min(actualTroop.Number, healthyCount);
var updatedTroop = actualTroop;
updatedTroop. Number = finalCount;
_actualTroopRoster.Add(updatedTroop);
}
}
public override void SyncData(IDataStore dataStore)
{
dataStore.SyncData("actualTroopRoster", ref _actualTroopRoster);
}
public static class EncounterGameMenuBehaviorPatch
{
private static bool isPlayerArmyBig()
{
if (PlayerEncounter.Battle != null && PlayerEncounter.Battle.IsFinalized)
return false;
if (MobileParty.MainParty.Army != null &&
MobileParty.MainParty.Army.LeaderParty.Name != MobileParty.MainParty.Name)
return false;
if (PlayerEncounter.Battle != null && PlayerSiege.BesiegedSettlement == null &&
MobileParty.MainParty.Army == null &&
PlayerEncounter.Battle.PartiesOnSide(PlayerEncounter.Battle.PlayerSide).Count(x => !x.IsNpcParty) >
1 &&
PlayerEncounter.Battle.GetLeaderParty(PlayerEncounter.Battle.PlayerSide).Name !=
PartyBase.MainParty.Name)
return false;
if (PlayerEncounter.Battle != null || MapEvent.PlayerMapEvent != null ||
PlayerEncounter.EncounteredParty?.MapEvent != null)
{
MapEvent encounteredBattle = PlayerEncounter.Battle ??
MapEvent.PlayerMapEvent ?? PlayerEncounter.EncounteredParty?.MapEvent;
_playerSide = encounteredBattle.PlayerSide;
try
{
PlayerArmySize = encounteredBattle.GetMapEventSide(encounteredBattle.PlayerSide)
.GetTotalHealthyTroopCountOfSide();
EnemyArmySize = encounteredBattle
.GetMapEventSide(encounteredBattle.PlayerSide.GetOppositeSide())
.GetTotalHealthyTroopCountOfSide();
}
catch (Exception)
{
return false;
}
}
else if (PlayerSiege.BesiegedSettlement != null)
{
PlayerArmySize = PlayerSiege.PlayerSiegeEvent.GetSiegeEventSide(PlayerSiege.PlayerSide)
.GetInvolvedPartiesForEventType().Sum(x => x.NumberOfHealthyMembers);
EnemyArmySize = PlayerSiege.PlayerSiegeEvent
.GetSiegeEventSide(PlayerSiege.PlayerSide.GetOppositeSide())
.GetInvolvedPartiesForEventType().Sum(x => x.NumberOfHealthyMembers);
_playerSide = PlayerSiege.PlayerSide;
}
else
{
InformationManager.DisplayMessage(
new InformationMessage(
"ChooseYourTroops Error: Enemy party or besieged settlement not found."));
return false;
}
return true;
}
public static void Postfix(CampaignGameStarter gameSystemInitializer)
{
gameSystemInitializer.AddGameMenuOption("encounter", "select_troops",
"{=select_your_troops_text}Select your troops.".ToString(), delegate(MenuCallbackArgs args)
{
args.optionLeaveType = GameMenuOption.LeaveType.TroopSelection;
args.IsEnabled = true;
bool condition = isPlayerArmyBig();
if (PlayerArmySize <= 1)
{
args.IsEnabled = false;
args.Tooltip =
new TextObject(
"{=select_your_troops_tooltip}There is only one person or less on your party.");
}
return condition;
}, new GameMenuOption.OnConsequenceDelegate(OpenTroopsSelector));
gameSystemInitializer.AddGameMenuOption("menu_siege_strategies", "select_troops_menu_siege_strategies",
"{=select_your_troops_text}Select your troops.", delegate(MenuCallbackArgs args)
{
args.optionLeaveType = GameMenuOption.LeaveType.TroopSelection;
args.IsEnabled = true;
bool condition = isPlayerArmyBig();
if (PlayerArmySize <= 1)
{
args.IsEnabled = false;
args.Tooltip =
new TextObject(
"{=select_your_troops_tooltip}There is only one person or less on your party.");
}
return condition;
}, new GameMenuOption.OnConsequenceDelegate(OpenTroopsSelector));
}
static bool CanChangeStatusOfTroop(CharacterObject character) => !character.IsPlayerCharacter;
static void OnDone(TroopRoster troopRoster, MenuCallbackArgs args)
{
if (troopRoster.TotalManCount < initialPlayerSpawn)
initialPlayerSpawn = troopRoster.TotalManCount;
if (!_haveChosenTroops)
InformationManager.DisplayMessage(new InformationMessage(
new TextObject("{=troops_selected_text}Troops selected!").ToString(), new Color(15, 70, 160)));
_actualTroopRoster = troopRoster;
_haveChosenTroops = true;
}
public static void OpenTroopSelection(MenuCallbackArgs args, TroopRoster fullRoster,
TroopRoster initialSelections, Func<CharacterObject, bool> canChangeChangeStatusOfTroop,
Action<TroopRoster> onDone, int maxSelectableTroopCount, int minSelectableTroopCount)
{
if (args.MenuContext.Handler != null)
{
MenuViewContext menuView = args.MenuContext.Handler as MenuViewContext;
if (menuView != null)
{
AccessTools.Field(typeof(MenuViewContext), "_menuTroopSelection").SetValue(menuView,
menuView.AddMenuView<CYTGauntletMenuTroopSelectionView>(fullRoster, initialSelections,
canChangeChangeStatusOfTroop, onDone, maxSelectableTroopCount,
minSelectableTroopCount));
}
}
}
private static void OpenTroopsSelector(MenuCallbackArgs args)
{
CheckTroopRoster();
int initialPlayerTroops = PlayerArmySize;
if (PlayerArmySize + EnemyArmySize > ChooseYourTroopsConfig.CurrentBattleSizeLimit)
{
if (PlayerArmySize > ChooseYourTroopsConfig.CurrentBattleSizeLimit / 2 ||
EnemyArmySize > ChooseYourTroopsConfig.CurrentBattleSizeLimit / 2)
{
if (EnemyArmySize < PlayerArmySize)
{
initialPlayerTroops = ChooseYourTroopsConfig.CurrentBattleSizeLimit / 2 +
(ChooseYourTroopsConfig.CurrentBattleSizeLimit / 2 - EnemyArmySize);
}
if (PlayerArmySize > ChooseYourTroopsConfig.CurrentBattleSizeLimit / 2 &&
EnemyArmySize > ChooseYourTroopsConfig.CurrentBattleSizeLimit / 2)
initialPlayerTroops = ChooseYourTroopsConfig.CurrentBattleSizeLimit / 2;
}
}
TroopRoster armyRoster = MobileParty.MainParty.MemberRoster;
if (MobileParty.MainParty.Army != null &&
MobileParty.MainParty.Army.LeaderParty.Name == MobileParty.MainParty.Name)
{
armyRoster = TroopRoster.CreateDummyTroopRoster();
foreach (MobileParty party in MobileParty.MainParty.Army.Parties)
{
armyRoster.Add(party.MemberRoster);
}
}
else if ((PlayerEncounter.Battle != null &&
PlayerEncounter.Battle.PartiesOnSide(_playerSide).Count > 1) ||
(PlayerSiege.BesiegedSettlement != null &&
PlayerSiege.PlayerSiegeEvent.GetSiegeEventSide(PlayerSiege.PlayerSide)
.GetInvolvedPartiesForEventType().Count() > 1) && MobileParty.MainParty.Army == null)
{
armyRoster = TroopRoster.CreateDummyTroopRoster();
List<PartyBase> PartiesOnPlayerSide = PlayerEncounter.Battle != null
? PlayerEncounter.Battle.PartiesOnSide(_playerSide).Select(x => x.Party).ToList()
: PlayerSiege.PlayerSiegeEvent.GetSiegeEventSide(PlayerSiege.PlayerSide)
.GetInvolvedPartiesForEventType().ToList();
foreach (PartyBase party in PartiesOnPlayerSide)
{
armyRoster.Add(party.MemberRoster);
}
}
initialPlayerSpawn = initialPlayerTroops;
// First gets only the healthy and not hero troops
TroopRoster dummyTroopRoster = TroopRoster.CreateDummyTroopRoster();
foreach (var troop in armyRoster.GetTroopRoster().Where(x => x.Character.IsHero && x.Number > x.WoundedNumber))
{
dummyTroopRoster.Add(troop);
}
if (_actualTroopRoster != null)
{
var soldiersRoster = _actualTroopRoster.CloneRosterData();
foreach (var soldier in soldiersRoster.GetTroopRoster())
{
if (soldier.WoundedNumber > 0)
{
soldiersRoster.AddToCounts(soldier.Character, -soldier.WoundedNumber);
}
}
if (soldiersRoster.TotalManCount + dummyTroopRoster.TotalManCount > initialPlayerTroops)
{
for (int i = initialPlayerTroops - 1 - dummyTroopRoster.TotalManCount; i < soldiersRoster.Count + dummyTroopRoster.TotalManCount - initialPlayerTroops; i++)
{
soldiersRoster.RemoveTroop(soldiersRoster.GetCharacterAtIndex(i), 1);
}
}
foreach (var troop in soldiersRoster.GetTroopRoster().Where(x => !x.Character.IsHero))
{
dummyTroopRoster.AddToCounts(troop.Character, troop.Number - troop.WoundedNumber);
}
}
else
{
TroopRoster strongestAndPriorTroops =
MobilePartyHelper.GetStrongestAndPriorTroops(MobileParty.MainParty,
initialPlayerTroops - dummyTroopRoster.TotalManCount, false);
strongestAndPriorTroops.RemoveIf(x => x.Character.IsHero);
dummyTroopRoster.Add(strongestAndPriorTroops);
}
OpenTroopSelection(args, armyRoster, dummyTroopRoster,
new Func<CharacterObject, bool>(CanChangeStatusOfTroop),
delegate(TroopRoster roster) { OnDone(roster, args); }, initialPlayerTroops, 1);
}
public static Dictionary<UniqueTroopDescriptor, MapEventParty> allocatedTroops;
[HarmonyPatch(typeof(MapEventSide), "AllocateTroops")]
static class AllocateTroopsPrefix
{
[HarmonyPrefix]
static void Prefix(ref List<UniqueTroopDescriptor> troopsList, ref int number,
ref Func<UniqueTroopDescriptor, MapEventParty, bool> customAllocationConditions,
MapEventSide __instance)
{
try
{
if (_setupSide && _actualTroopRoster != null && !__instance.MapEvent.IsPlayerSimulation && number == initialPlayerSpawn)
{
if (__instance.MissionSide == _playerSide && PlayerArmySize > 0)
{
allocatedTroops = new();
FlattenedTroopRoster flattenedTroopRoster = _actualTroopRoster.ToFlattenedRoster();
List<(FlattenedTroopRosterElement, MapEventParty, float)> list =
(List<(FlattenedTroopRosterElement, MapEventParty, float)>)AccessTools
.Field(typeof(MapEventSide), "_readyTroopsPriorityList").GetValue(__instance);
_actualReadyTroopsPriorityList = list.AsReadOnly().ToList();
customAllocationConditions = (descriptor, party) =>
{
FlattenedTroopRosterElement troop = flattenedTroopRoster.FirstOrDefault(x =>
x.Troop.StringId == party.Troops[descriptor].Troop?.StringId);
if (!troop.Equals(default(FlattenedTroopRosterElement)))
{
allocatedTroops.Add(descriptor, party);
flattenedTroopRoster.Remove(troop.Descriptor);
return true;
}
return false;
};
}
}
}
catch (Exception e)
{
InformationManager.DisplayMessage(
new InformationMessage("ChooseYourTroops Error at AllocateTroops Prefix"));
}
}
}
[HarmonyPatch(typeof(MapEventSide), "AllocateTroops")]
static class AllocateTroopsPostfix
{
[HarmonyPostfix]
static void Postfix(ref List<UniqueTroopDescriptor> troopsList, int number,
Func<UniqueTroopDescriptor, MapEventParty, bool> customAllocationConditions,
MapEventSide __instance)
{
if (_setupSide && __instance.MissionSide == _playerSide && number == initialPlayerSpawn &&
_actualTroopRoster != null)
{
try
{
_actualReadyTroopsPriorityList.RemoveAll(
x => allocatedTroops.ContainsKey(x.Item1.Descriptor));
AccessTools.Field(typeof(MapEventSide), "_readyTroopsPriorityList")
.SetValue(__instance, _actualReadyTroopsPriorityList);
AccessTools.Field(typeof(MapEventSide), "_allocatedTroops")
.SetValue(__instance, allocatedTroops);
}
catch (Exception e)
{
InformationManager.DisplayMessage(
new InformationMessage("ChooseYourTroops Error at AllocateTroops Postfix"));
_actualReadyTroopsPriorityList = null;
}
}
}
}
[HarmonyPatch(typeof(PlayerEncounter), "FinishEncounterInternal")]
static class PlayerEncounterPrefix
{
[HarmonyPrefix]
static void Prefix()
{
_playerSide = BattleSideEnum.None;
_setupSide = false;
_actualReadyTroopsPriorityList = null;
_haveChosenTroops = false;
}
}
[HarmonyPatch(typeof(MissionAgentSpawnLogic), "AfterStart")]
static class AfterStartPostfix
{
[HarmonyPostfix]
static void Postfix(MissionAgentSpawnLogic __instance)
{
if (_haveChosenTroops)
{
_setupSide = true;
}
}
}
[HarmonyPatch(typeof(LordsHallFightMissionController), "OnCreated")]
static class InitializeMissionPrefix
{
[HarmonyPrefix]
static void Prefix()
{
if (_setupSide)
{
_setupSide = false;
_haveChosenTroops = false;
}
}
}
[HarmonyPatch(typeof(Mission), "OnBattleSideDeployed")]
static class OnBattleSideDeployedPostfix
{
[HarmonyPostfix]
static void Postfix(BattleSideEnum side)
{
if (side == _playerSide)
PlayerArmySize = 0;
else
EnemyArmySize = 0;
}
}
private static int EnemyInitialSpawn => EnemyArmySize <= ChooseYourTroopsConfig.CurrentBattleSizeLimit - initialPlayerSpawn
? EnemyArmySize
: ChooseYourTroopsConfig.CurrentBattleSizeLimit - initialPlayerSpawn;
/** Used to set the initial spawn of the player or the enemy taking into account if troops with mount should be counted as two */
private static int GetEnemyInitialSpawn(IEnumerable<FlattenedTroopRosterElement> troops)
{
int playerInitialAgents = 0;
foreach (var flattenedTroopRosterElement in _actualTroopRoster.ToFlattenedRoster())
{
playerInitialAgents += DoesTroopCountByTwo(flattenedTroopRosterElement.Troop) ? 2 : 1;
}
int maxAgents = ChooseYourTroopsConfig.CurrentBattleSizeLimit - playerInitialAgents;
int finalAmount = 0;
int totalAgents = 0;
for (int i = 0; i < troops.Count(); i++)
{
int currentAgents = DoesTroopCountByTwo(troops.ElementAt(i).Troop) ? 2 : 1;
if (totalAgents + currentAgents > maxAgents)
{
break;
}
totalAgents += currentAgents;
finalAmount += 1;
}
return finalAmount;
}
private static readonly Type MissionSideType = AccessTools.TypeByName("TaleWorlds.MountAndBlade.MissionAgentSpawnLogic+MissionSide");
[HarmonyPatch(typeof(MissionAgentSpawnLogic), "Init")]
static class MissionAgentSpawnLogicInitdPostfix
{
[HarmonyPostfix]
static void Postfix(bool spawnDefenders, bool spawnAttackers,
in MissionSpawnSettings reinforcementSpawnSettings, MissionAgentSpawnLogic __instance)
{
try
{
if (_haveChosenTroops)
{
var missionSidesField = AccessTools.Field(typeof(MissionAgentSpawnLogic), "_missionSides");
var missionSides = (Array)missionSidesField.GetValue(__instance);
var phases = __instance.GetType()
.GetField("_phases", BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue(__instance);
object[] phasesArray = (object[])phases;
int enemyInitialSpawnCount = initialPlayerSpawn;
for (int i = 0; i < phasesArray.Length; i++)
{
List<object> list = Enumerable.Cast<object>((IEnumerable)phasesArray[i]).ToList();
FieldInfo initialSpawnNumberProp =
AccessTools.Field(list[0].GetType(), "InitialSpawnNumber");
bool isPlayerSide = i == 0 ? (_playerSide == BattleSideEnum.Defender) : (_playerSide == BattleSideEnum.Attacker);
int currentInitialSpawn;
if (isPlayerSide)
{
currentInitialSpawn = initialPlayerSpawn;
}
else
{
if (ChooseYourTroopsConfig.Instance is { SupportForMaximumTroops: true })
{
var troopSupplier = (PartyGroupTroopSupplier)AccessTools
.Field(MissionSideType, "_troopSupplier").GetValue(missionSides.GetValue(i));;
var partyGroup = (MapEventSide)AccessTools.Property(typeof(PartyGroupTroopSupplier), "PartyGroup").GetValue(troopSupplier);
var readyTroopsPriorityList = (List<ValueTuple<FlattenedTroopRosterElement, MapEventParty, float>>)AccessTools.Field(typeof(MapEventSide), "_readyTroopsPriorityList").GetValue(partyGroup);
readyTroopsPriorityList =
readyTroopsPriorityList.OrderByDescending(x => x.Item3).ToList();
currentInitialSpawn = GetEnemyInitialSpawn(readyTroopsPriorityList.Select(item => item.Item1));
}
else
{
currentInitialSpawn = EnemyInitialSpawn;
}
enemyInitialSpawnCount = currentInitialSpawn;
}
initialSpawnNumberProp.SetValue(list[0], currentInitialSpawn);
AccessTools.Field(list[0].GetType(), "RemainingSpawnNumber").SetValue(list[0],
isPlayerSide
? PlayerArmySize - initialPlayerSpawn
: EnemyArmySize - EnemyInitialSpawn);
}
AccessTools.Method(Mission.Current.GetType(), "SetBattleAgentCount", null, null).Invoke(Mission.Current, new object[]
{
Math.Min(initialPlayerSpawn, enemyInitialSpawnCount)
});
object missionSidesObj = missionSidesField.GetValue(__instance);
Array missionSidesArray = missionSidesObj as Array;
if (missionSidesArray != null)
{
object defenderSide = missionSidesArray.GetValue(0);
object attackerSide = missionSidesArray.GetValue(1);
MethodInfo setSpawnMethod = AccessTools.Method(defenderSide.GetType(), "SetSpawnTroops");
setSpawnMethod.Invoke(defenderSide, new object[] { spawnDefenders });
setSpawnMethod.Invoke(attackerSide, new object[] { spawnAttackers });
}
}
}
catch (Exception ex)
{
InformationManager.DisplayMessage(
new InformationMessage("ChooseYourTroops Error at MissionAgentSpawnLogic Postfix"));
_setupSide = false;
}
}
}
}
}
public class SubModule : MBSubModuleBase
{
private Harmony _harmony;
private bool _gameMenuPatch = false;
protected override void OnSubModuleLoad()
{
base.OnSubModuleLoad();
_harmony = new Harmony("choose_your_troops");
_harmony.PatchAll();
}
public override void OnGameLoaded(Game game, object initializerObject)
{
base.OnCampaignStart(game, initializerObject);
CampaignGameStarter gameStarter = (CampaignGameStarter)initializerObject;
gameStarter.AddBehavior(new ChooseYourTroopsBehavior());
if (!_gameMenuPatch)
{
var original = AccessTools.Method(typeof(EncounterGameMenuBehavior), "AddGameMenus");
var postfix = AccessTools.Method(typeof(ChooseYourTroopsBehavior.EncounterGameMenuBehaviorPatch), "Postfix");
_harmony.Patch(original, null, postfix);
_gameMenuPatch = true;
}
}
}
}